Key Press through Batch File

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Niraj
Posts: 2
Joined: 19 Jul 2013 01:26

Key Press through Batch File

#1 Post by Niraj » 19 Jul 2013 01:35

Hi,

Want to access some key board Button through Batch script.

Example: Suppose open notepad through script,and Want to access
Ctrl+S : For saving the file
Ctrl+P : For Print that file

or some some simple key opertion.

like Write some already define content.

Thanks in Advance

Regards,
Niraj

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Key Press through Batch File

#2 Post by foxidrive » 19 Jul 2013 03:49

The program AutoIt is a good tool for pressing buttons in an application.

npocmaka_
Posts: 512
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: Key Press through Batch File

#3 Post by npocmaka_ » 19 Jul 2013 05:21

You can use wscript sendkeys function.and AppActivate to send the keys to an application:

http://social.technet.microsoft.com/wik ... ethod.aspx

here's a sendkeys.bat:

Code: Select all

@if (@X)==(@Y) @end /* Harmless hybrid line that begins a JScript comment
@echo off
cscript //E:JScript //nologo "%~f0" %*

exit /b 0

***jscript part****/

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

WshShell.AppActivate(args.Item(0));
//sleep(1500)
WshShell.SendKeys(args.Item(1));

function sleep(milliseconds) {
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i++) {
    if ((new Date().getTime() - start) > milliseconds){
      break;
    }
  }
}


/*http://social.technet.microsoft.com/wiki/contents/articles/5169.vbscript-sendkeys-method.aspx*/


Does not check command line arguments but this can be added easy.First argument is the image name of the .exe you want to send keys to and the second is are the keys:

Code: Select all

sendkeys.bat notepad {space}{tab}{enter}blabla

Post Reply