Introduction
In Windows operating system, any program can run through a batch program. Also, it is very hard to press any key through a batch program. But it is possible indirectly by a batch program using VBScript.
Background
VBScript has a library to press any key such as "WshShell
".
Using the Code
Suppose a user opens Notepad and writes something. Now the user wants to save the file automatically through a batch program. In this situation, the following procedure can be followed:
- Create a VB script file named program.vbs. Write the following code and save it:
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.AppActivate "notepad"
WshShell.SendKeys "{ENTER}"
WshShell.SendKeys "^s"
where “notepad
” is the Notepad application which is already running. WshShell.SendKeys
is using the keypress dynamically. ^s means "Ctrl+s".
- Create a batch file named auto.bat and write the following code and save it:
wscript "F:\Ict_Backup\Desktop\program.vbs"
- Run the batch file. It will ask to save the file.
- Another Example: Launch Firefox browser full screen automatically (sleep is used for wait until application is running on the system.)
Set oShell = CreateObject("WScript.Shell")
oShell.Run("""C:\Program Files\Mozilla Firefox\firefox.exe""")
WScript.Sleep 3000
oShell.AppActivate "firefox"
WScript.Sleep 3000
oShell.SendKeys "~"
oShell.SendKeys "{F11}"
List of key codes for VBScript:
Key | Code |
Break | {BREAK} |
|
Backspace | {Backspace}, {BKSP} or {BS} |
|
Delete | {DELETE} or {DEL} |
|
Down Arrow | {DOWN} |
|
End | {END} |
|
Enter | {ENTER} or ~ |
|
Escape | {ESC} |
|
Help | {HELP} |
|
Home | {HOME} |
|
Insert | {INSERT} or {INS} |
|
Left Arrow | {LEFT} |
|
Num Lock | {NUMLOCK} |
|
Page Down | {PGDN} |
|
Page Up | {PGUP} |
|
Print Screen | {PRTSC} |
|
Right Arrow | {RIGHT} |
|
Scroll Lock | {SCROLLLOCK} |
|
Tab | {TAB} |
|
Up Arrow | {UP} |
|
F1 | {F1} |
|
F2 | {F2} |
|
F3 | {F3} |
|
F4 | {F4} |
|
F5 | {F5} |
|
F6 | {F6} |
|
F7 | {F7} |
|
F8 | {F8} |
|
F9 | {F9} |
|
F10 | {F10} |
|
F11 | {F11} |
|
F12 | {F12} |
|
F13 | {F13} |
|
F14 | {F14} |
|
F15 | {F15} |
|
F16 | {F16} |
|
Alt | {%} |
|
Ctrl | {^} |
|
Shift Lock | {+} |