Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

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

, 4 Sep 2013
Rate this:
Please Sign up or sign in to vote.
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:
    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 {+}

License

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

Share

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.
Follow on   Twitter

Comments and Discussions

 
GeneralGood Idea to Launch Browser Full Screen PinprofessionalClinton Gallagher3-Sep-13 11:16 
GeneralRe: Good Idea to Launch Browser Full Screen PinmemberGreen Mile3-Sep-13 17:37 
GeneralRe: Good Idea to Launch Browser Full Screen PinprofessionalClinton 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 PinmemberGreen Mile4-Sep-13 17:57 
GeneralRe: Good Idea to Launch Browser Full Screen PinprofessionalClinton Gallagher5-Sep-13 5:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web04 | 2.8.140916.1 | Last Updated 5 Sep 2013
Article Copyright 2013 by Green Mile
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid