Click here to Skip to main content
15,879,535 members
Articles / Programming Languages / VBScript
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
4.50/5 (2 votes)
4 Sep 2013CPOL1 min read 206.1K   5   5
Pressing any key automatically using batch program and VBScript

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:

  1. Create a VB script file named program.vbs. Write the following code and save it:
    VBScript
    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.)
    VBScript
    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{+}

License

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


Written By
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.

Comments and Discussions

 
GeneralGood Idea to Launch Browser Full Screen Pin
Clinton Gallagher3-Sep-13 11:16
professionalClinton Gallagher3-Sep-13 11:16 
GeneralRe: Good Idea to Launch Browser Full Screen Pin
Green Mile3-Sep-13 17:37
Green Mile3-Sep-13 17:37 
GeneralRe: Good Idea to Launch Browser Full Screen Pin
Clinton Gallagher4-Sep-13 4:13
professionalClinton Gallagher4-Sep-13 4:13 
The snippet above implies you modified the system path variable otherwise how could WshShell.AppActivate find firefox?

I'll try to make time to mess around with this but I hope somebody else who has the time will also mess around with it because launching browsers fullscreen at startup usually requires a Windows Service.
Clinton Gallagher

GeneralRe: Good Idea to Launch Browser Full Screen Pin
Green Mile4-Sep-13 17:57
Green Mile4-Sep-13 17:57 
GeneralRe: Good Idea to Launch Browser Full Screen Pin
Clinton Gallagher5-Sep-13 5:05
professionalClinton Gallagher5-Sep-13 5: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.