How to Press Buttons in Batch File
By Jeff Grundy
Updated April 06, 2018
Batch files allow you to automate many computer tasks by saving a list of commands in a “.bat” file. You can automate DOS commands easily in a batch file and schedule the tasks to run at specified times using the Windows Task Scheduler. For example, you might want to create a batch file using DOS commands to copy files from one folder to another. Batch files for DOS commands are easy to create because there are no mouse clicks to simulate or buttons to press. On the other hand, you cannot create a batch that automates mouse clicks inside a Windows program, but if the program allows keyboard shortcuts (almost all do), then you can use special DOS commands to send keystrokes in the application programmatically.
Open Windows Notepad or another text editor program on your computer.
Enter command-line syntax to open an application. For example, to open Microsoft Word 2010 from the command line, you would enter:
cd C:\Program Files (x86)\Microsoft Office\Office14 start /w WinWord.exe
The command-line text opens Microsoft Word 2010 and halts the continued execution of the batch file until Word completely loads. (Note: be sure to press the “Enter” key after each line of text.)
Enter a command to simulate a mouse click inside the open application. For instance, to enter a batch file command that simulates the mouse clicks used to create a new document file in Microsoft Word 2010, enter the following command text under the first line created earlier:
WshShell.Sendkeys "%f"
(simulates clicking “File” on the menu bar)
WshShell.Sendkeys “n”
(simulates clicking the “New” option on the “File” menu)
WshShell.Sendkeys "{Enter}"
(simulates clicking “OK” to finish creating a new document in Word)
Enter additional click commands as needed. Use the “%” symbol to simulate the “Alt” key used in using keyboard shortcuts instead of clicking icons with a mouse. For instance, you can use the “Alt” + “F” + “P” keys to access the Word “Print window. “ Using the keyboard shortcut is the same as clicking “File" > "Print” with the mouse. To simulate pressing the “Alt” + “F” + “P” keys in a batch file, you would enter:
WshShell.Sendkeys "%f"
(simulates clicking “File” on the menu bar)
WshShell.Sendkeys “p”
(simulates clicking the “Print” option on the “File” menu)
WshShell.Sendkeys "{Enter}"
(simulates clicking “OK” to send the Word document to the printer)
Save the batch file with a descriptive file name. Use the “.bat” file name extension, instead of the default “.txt” extension. The batch file automatically performs mouse clicks according to the command-line text you specified.
Tips
SendKeys is a powerful part of the Windows Script Host engine. You can use SendKeys to simulate thousands of different keystroke combinations in batch files and send them to both DOS and Windows applications. Refer to the Microsoft Developer Network for a full list of SendKeys syntax options and usage methods (see Resources).
References
Writer Bio
Jeff Grundy has been writing computer-related articles and tutorials since 1995. Since that time, Grundy has written many guides to using various applications that are published on numerous how-to and tutorial sites. Born and raised in South Georgia, Grundy holds a Master of Science degree in mathematics from the Georgia Institute of Technology.