33

I am trying to develop a batch file which can automatically press left arrow and right arrow key for n number of times with some pause in between. Could anybody please help me with this?

P.S: I tried installing auto keyboard software but I couldn't install them since I was at work. I need this program to work on my office PC.

Community
  • 1
  • 1
Rupesh
  • 643
  • 2
  • 9
  • 15

2 Answers2

55

Wow! Mean this that you must learn a different programming language just to send two keys to the keyboard? There are simpler ways for you to achieve the same thing. :-)

The Batch file below is an example that start another program (cmd.exe in this case), send a command to it and then send an Up Arrow key, that cause to recover the last executed command. The Batch file is simple enough to be understand with no problems, so you may modify it to fit your needs.

@if (@CodeSection == @Batch) @then


@echo off

rem Use %SendKeys% to send keys to the keyboard buffer
set SendKeys=CScript //nologo //E:JScript "%~F0"

rem Start the other program in the same Window
start "" /B cmd

%SendKeys% "echo off{ENTER}"

set /P "=Wait and send a command: " < NUL
ping -n 5 -w 1 127.0.0.1 > NUL
%SendKeys% "echo Hello, world!{ENTER}"

set /P "=Wait and send an Up Arrow key: [" < NUL
ping -n 5 -w 1 127.0.0.1 > NUL
%SendKeys% "{UP}"

set /P "=] Wait and send an Enter key:" < NUL
ping -n 5 -w 1 127.0.0.1 > NUL
%SendKeys% "{ENTER}"

%SendKeys% "exit{ENTER}"

goto :EOF


@end


// JScript section

var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys(WScript.Arguments(0));

For a list of key names for SendKeys, see: http://msdn.microsoft.com/en-us/library/8c6yea83(v=vs.84).aspx

For example:

LEFT ARROW    {LEFT}
RIGHT ARROW   {RIGHT}

For a further explanation of this solution, see: GnuWin32 openssl s_client conn to WebSphere MQ server not closing at EOF, hangs

Community
  • 1
  • 1
Aacini
  • 59,374
  • 12
  • 63
  • 94
  • This is great! Thanks so much. I used it e.g. to prepare the Citrix 2013 login (script line to start and activate the login url: `@start iexplore https:///Citrix/StoreFrontWeb` ... – Kawi42 Apr 17 '15 at 08:36
  • this requires the console window to be focused while the keys are "pressed" – jan-glx Oct 14 '16 at 11:36
  • using `TITLE blablablub` in the batch part and `WshShell.AppActivate("blablablub"); WScript.Sleep(100);` in the Jscript part would reduce the problem a little bit. – jan-glx Oct 14 '16 at 13:05
  • 8
    could you tell me what `set SendKeys=CScript //nologo //E:JScript "%~F0"` is for? – Kayracer Apr 22 '17 at 15:20
  • I'm folllowing @Kayracer's question. – AyukNayr Oct 08 '18 at 06:59
  • @Kayracer: This variable is defined just to use `%SendKeys%` form instead of `CScript //nologo //E:JScript "%~F0"`; the first one is clearer. For further information about JScript, see [this site](https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/scripting-articles/yek4tbz0(v=vs.84)). – Aacini Oct 08 '18 at 13:00
6

Just to be clear, you are wanting to launch a program from a batch file and then have the batch file press keys (in your example, the arrow keys) within that launched program?

If that is the case, you aren't going to be able to do that with simply a ".bat" file as the launched would stop the batch file from continuing until it terminated--

My first recommendation would be to use something like AutoHotkey or AutoIt if possible, simply because they both have active forums where you'd find countless examples of people launching applications and sending key presses not to mention tools to simply "record" what you want to do. However you said this is a work computer and you may not be able to load a 3rd party program.. but you aren't without options.

You can use Windows Scripting Host from something like a .vbs file to launch a program and send keys to that process. If you're running a version of Windows that includes PowerShell 2.0 (Windows XP with Service Pack 3, Windows Vista with Service Pack 1, Windows 7, etc.) you can use Windows Scripting Host as a COM object from your PS script or use VB's Intereact class.

The specifics of how to do it are outside the scope of this answer but you can find numerous examples using the methods I just described by searching on SO or Google.

edit: Just to help you get started you can look here:

  1. Automate tasks with Windows Script Host's SendKeys method
  2. A useful thread about SendKeys