0

Im super new to programming, falling in and out from time to time. But once in a blue moon, I want to add functionality to my keyboard, in attempt to make my life a little easier. So what I want to do, is ALT + NumPad x to start a program, and if that program is open when ALT + NumPad x is pressed, close the program.

set program1="chrome"
start %program1%

That will open the program, but what should I do to close it? Make a variable program'x'open = true/false/ if true call a function to close, else nothing? Im stuck on the close program part.

Machavity
  • 30,841
  • 27
  • 92
  • 100
zacsloth
  • 31
  • 1
  • 7

3 Answers3

0

try it with the TSKILL or TASKKILL command e.g.: tskill chrome.exe - you'll need to know the processname It's not possible to close a program the same way as clicking on the X button on the title bar because by clicking this button, an action IN that program is performed. You can't intervene in other programs using the command line.

0

This is some code for terminating a running program:

@echo off
title TASK KILLER
color 0A
:Boot_Up
echo Hello!
echo Welcome to TASK KILLER, the function of 
echo which is to terminate running programs!
pause
goto TASK_KILLER
:TASK_KILLER
cls
echo Please name the task you want to terminate!
echo Example: to terminate Notepad, type notepad.
echo THIS ONLY TERMINATES EXE PROGRAMS!
set /p terminate=
taskkill /im %terminate%.exe /t /f
pause
echo TERMINATED!
pause
goto TASK_KILLER

It can terminate any program, I advise running as an administrator, and know what programs are essential to your computer. Now I suppose you want an opening system to? Well here it all is!

@echo off
title TASK KILLER AND OPENER
color 0A
:Boot_Up
echo Hello!
echo Welcome to TASK KILLER AND OPENER, the function of 
echo which is to terminate running programs and open new ones!
echo Type Kill to go to the terminate section, and type Open to go to the opener!
set /p answer=
if %answer% equ Kill goto TASK_KILLER
if %answer% equ Open goto TASK_OPENER
if %answer% neq Kill goto Boot_Up
:TASK_OPENER
cls
echo Please specify where the program is in your computer.
echo Example: I:\Code\MyProgram
set /p directory=
cd %directory%
cls
echo Is the program in %cd%?
echo Yes (1) or No (2)
set /p answer=
if %answer% equ 1 goto TASK_OPENER2
if %answer% equ 2 goto TASK_OPENER
if %answer% neq 1 goto TASK_OPENER
:TASK_OPENER2
cls
echo Please name the program.
echo Example: vlc.exe/MyCode.bat
echo This can open .bat, .exe, etcetera.
set /p program=
start %program%
pause
echo DONE!
pause
goto Boot_Up
:TASK_KILLER
cls
echo Please name the task you want to terminate!
echo Example: to terminate Notepad, type notepad
echo THIS ONLY TERMINATES EXE PROGRAMS!
set /p terminate=
taskkill /im %terminate%.exe /t /f
pause
echo TERMINATED!
pause
goto Boot_Up

That's how you do it! I hope I helped!

ender_scythe
  • 470
  • 7
  • 16
0

Windows provides a number of ways to launch programs from the keyboard without a batch file at all. The simplest is to pin Chrome to the taskbar. You can use Win+1 to launch the first icon on the taskbar, Win+2 to launch the second, and so-on.

You can also launch any shortcut with a custom Alt or Ctrl combo. Create a shortcut to Chrome (doesn't matter where). Right-click the shortcut and choose Properties. Click inside the text box labeled Shortcut key, and then type the keystroke you want to launch it.

Shortcut key

When done, you can use Alt+F4 to close the program from the keyboard.

Finally, if the same key needs to both launch and close it, you can use the shortcut method above with a batch file. Put the following into a batch file somewhere, say C:\batch\ToggleChrome.cmd

tasklist | findstr "chrome.exe" && taskkill /im chrome.exe || start d:\path\to\chrome.exe

Now create a shortcut (right-click, new, Shortcut). The shortcut command should be:

cmd /c c:\batch\ToggleChrome.bat

Once you have the shortcut, right-click and use the Properties dialog to set a shortcut key.

Ryan Bemrose
  • 9,018
  • 1
  • 41
  • 54