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

Tagged as

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

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 12:16 
GeneralRe: Good Idea to Launch Browser Full Screen PinmemberGreen Mile3-Sep-13 18:37 
GeneralRe: Good Idea to Launch Browser Full Screen PinprofessionalClinton Gallagher4-Sep-13 5:13 
GeneralRe: Good Idea to Launch Browser Full Screen PinmemberGreen Mile4-Sep-13 18:57 
Hi Clinton,
 
I have tried to open firefox with full screen. Good news is I've done it by a small trick. Here is the code.
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}"
Just using sleep to wait until application running on the system.
 
From your comment: "The snippet above implies you modified the system path variable otherwise how could WshShell.AppActivate find firefox?"
Answer: If an application already running in system then WSCript can find that application by its process name. You can check the process name by task manager after running an application manually.
 
I hope, Now you can open Firefox in full screen automatically. If not then try increasing sleep value.
 

Thanks again for your comment.
GeneralRe: Good Idea to Launch Browser Full Screen PinprofessionalClinton Gallagher5-Sep-13 6: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 | Terms of Use | Mobile
Web03 | 2.8.150327.1 | Last Updated 5 Sep 2013
Article Copyright 2013 by Green Mile
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid