Click here to Skip to main content
12,622,652 members (28,098 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


6 bookmarked

Press Any Key Automatically Using Batch Program by Opening Existing User Running Application

, 4 Sep 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
Pressing any key automatically using batch program and VBScript


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.


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:

  1. 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".

  2. Create a batch file named auto.bat and write the following code and save it:
    wscript "F:\Ict_Backup\Desktop\program.vbs"
  3. Run the batch file. It will ask to save the file.
  4. 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 {+}


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Green Mile
Software Developer (Senior) BRAC ICT
Bangladesh Bangladesh
Shaiful Islam(Palash) is dedicated, energetic, trusted, top-producing software Engineer with over 5+ years experience providing programming expertise. He is experienced in designing and developing Object Oriented solutions using Java/J2EE technology, SaaS application, SSO service, Mobile based App(Android), Travel & Courier application. He is the founder of V for Vagabond Lab. He started his career at Metafour Asia Ltd as a Software developer. Currently, He is working at BRAC ICT as senior software engineer.

You may also be interested in...


Comments and Discussions

GeneralGood Idea to Launch Browser Full Screen Pin
Clinton Gallagher3-Sep-13 12:16
professionalClinton Gallagher3-Sep-13 12:16 
GeneralRe: Good Idea to Launch Browser Full Screen Pin
Green Mile3-Sep-13 18:37
memberGreen Mile3-Sep-13 18:37 
GeneralRe: Good Idea to Launch Browser Full Screen Pin
Clinton Gallagher4-Sep-13 5:13
professionalClinton Gallagher4-Sep-13 5:13 
GeneralRe: Good Idea to Launch Browser Full Screen Pin
Green Mile4-Sep-13 18:57
memberGreen Mile4-Sep-13 18:57 
GeneralRe: Good Idea to Launch Browser Full Screen Pin
Clinton Gallagher5-Sep-13 6:05
professionalClinton Gallagher5-Sep-13 6:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161128.1 | Last Updated 5 Sep 2013
Article Copyright 2013 by Green Mile
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid