topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Monday March 18, 2024, 11:24 pm
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Last post Author Topic: Simple Commandline to send Keystrokes (WinSendKeys)  (Read 101601 times)

Split_e

  • Supporting Member
  • Joined in 2011
  • **
  • default avatar
  • Posts: 6
    • View Profile
    • Donate to Member
Simple Commandline to send Keystrokes (WinSendKeys)
« on: January 23, 2011, 06:50 PM »
This is continued from https://www.donation...ndex.php?topic=25388

I'm looking to use Screen Captor to basically screen cap every page of a book; the book software navigates to the next page via the Right Arrow key or Page Up key. Screen Captor can do the whole process, but only if I give it a proper commandline to invoke. So that brings me here.

Just looking for a couple of lines of code that I can plug into Screen Captor (in post-capture commandline option) to hit one key (either right arrow or page up) once. Thanks a ton in advance!
« Last Edit: February 11, 2011, 07:20 AM by mouser »

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,896
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Re: Simple Commandline to send Keystrokes
« Reply #1 on: January 23, 2011, 07:04 PM »
so just to refine your request,

what you are looking for is a commandline tool that can send arbitrary keys (like right arrow, pageup, etc.) to the last active window, or optionally to a window specified by title/class/applicationexefilename.

rjbull

  • Charter Member
  • Joined in 2005
  • ***
  • default avatar
  • Posts: 3,199
    • View Profile
    • Donate to Member
Re: Simple Commandline to send Keystrokes
« Reply #2 on: January 24, 2011, 10:22 AM »
Why does it have to be command line?  There's some reason you can't use a macro program like Macro Express or PowerPro?

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,896
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Re: Simple Commandline to send Keystrokes
« Reply #3 on: January 24, 2011, 01:19 PM »
it has to be a commandline utility because he's going to launch it from another program when it needs to be run.

rjbull

  • Charter Member
  • Joined in 2005
  • ***
  • default avatar
  • Posts: 3,199
    • View Profile
    • Donate to Member
Re: Simple Commandline to send Keystrokes
« Reply #4 on: January 24, 2011, 04:57 PM »
Looks like NirCmd might be able to do it.  At least, it has "sendkey" type commands, but it doesn't seem to specify a target program or window.

Also looks like AutoHotKey may be able to read text from a file and type it as keystrokes to a program, or, maybe you can use Windows Scripting Host.  However, most of the Google hits appear to refer to sending keystrokes to a command window, not getting them from it.

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,610
    • View Profile
    • Donate to Member
Re: Simple Commandline to send Keystrokes
« Reply #5 on: January 24, 2011, 04:58 PM »
Ok, I've not searched the internet, but just wanted to write a little AutoIt3 script that does exactly that for you:

Latest release & download moved to separate thread

WinSendKeys [-t Timedelay] [-d] [-w] <WindowName> <Keystrokes to send>
It takes 3 options and as much parameters (keystrokes) as you would like:
- -w <WindowName> : A window name or expression, as detailed in this page
- -t <Timedelay> : A number of milliseconds to wait between each set of keystrokes to send
- -d : Debug mode, writes some info into WinSendKeys.log in the current directory
- <Keystrokes to send> : Any amount of keystrokes that will fit the commandline, using the syntax from this page
(Be sure to double up any ^ marker for Control as it has a special feature in cmd.exe, so ^^n becomes Ctrl-N)

Updated to a newer version below, 1.0 is hiding here...
This is the script:
Code: AutoIt [Select]
  1. #Region ;**** Directives created by AutoIt3Wrapper_GUI ****
  2. #AutoIt3Wrapper_UseUpx=n
  3. #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
  4. ; WinSendKeys
  5. ; By Ath, 2011-01-24
  6. ; Updated 2011-01-25, upx disabled, debug mode writes to .log file, formatted helpmessage a little better
  7. ; options:
  8. ; -t timeout
  9. ; -w windowname
  10. ; -d : Debug mode
  11. ; Keystrokes: http://www.autoitscript.com/autoit3/docs/appendix/SendKeys.htm
  12.  
  13. $dela = 100 ; msec delay between keystrokes sent
  14. $win = "" ; Name of Window to activate, naming according to these rules: http://www.autoitscript.com/autoit3/docs/intro/windowsadvanced.htm
  15. $dbg = 0 ; Debug mode
  16. $dbgfile = "" ; Write logging to this file
  17.  
  18. If $CmdLine[0] < 2 Then
  19.         ShowHelp()
  20.  
  21.         $p = 1
  22.         $n = 1
  23.         While $n <= $CmdLine[0]
  24.                 If ($p = $n) And (StringLeft($CmdLine[$n],1) = "-") Then
  25.                         If (StringLower($CmdLine[$n]) = "-t") And ($n < $CmdLine[0]) Then
  26.                                 $n = $n + 1
  27.                                 $dela = $CmdLine[$n]
  28.                                 $p = $n + 1
  29.                         EndIf
  30.                         If (StringLower($CmdLine[$n])) = "-w" And ($n < $CmdLine[0]) Then
  31.                                 $n = $n + 1
  32.                                 $win = $CmdLine[$n]
  33.                                 $p = $n + 1
  34.                         EndIf
  35.                         If StringLower($CmdLine[$n]) = "-d" Then
  36.                                 $dbg = 1
  37.                                 $p = $n + 1
  38.                                 $dbgfile = StringReplace(@ScriptName,".au3",".log")
  39.                                 If $dbgfile = @ScriptName Then
  40.                                         $dbgfile = StringReplace(@ScriptName,".exe",".log")
  41.                                 EndIf
  42.                         EndIf
  43.                 EndIf
  44.                 $n = $n + 1
  45.         WEnd
  46.         If $win = "" Then
  47.                 $win = $CmdLine[$p]
  48.                 $p = $p + 1
  49.         EndIf
  50.         If Not $win = "" Then
  51.                 If $dbg Then writeLogLine("Window: " & $win & @CRLF)
  52.                 AutoItSetOption("WinTitleMatchMode",2)
  53.                 $w = WinActivate($win, "")
  54.                 If $dbg Then writeLogLine("Windowhandle: " & $w)
  55.                 If $w <> 0 Then
  56.                         SendKeepActive($w)
  57.                         For $n = $p to $CmdLine[0]
  58.                                 If $dbg Then writeLogLine("Send: " & $CmdLine[$n] & @CRLF)
  59.                                 Send($CmdLine[$n])
  60.                                 Sleep($dela)
  61.                         Next
  62.                 EndIf
  63.         EndIf
  64.  
  65. Func ShowHelp()
  66.         MsgBox(0,"WinSendKeys : Send keystrokes to a named window","Usage:" & @CRLF _
  67.         &       "WinSendKeys [-t keydelay] [-d] [-w] <WindowName> <Keystrokes to send>" & @CRLF _
  68.         & "-t keydelay = msec value, default 100" & @CRLF _
  69.         & "-d = Debug mode, writes extra information to WinSendKeys.log" & @CRLF _
  70.         & "-w <WindowName> = name value as defined here:" & @CRLF _
  71.         & @TAB & "www.autoitscript.com/autoit3/docs/intro/windowsadvanced.htm" & @CRLF _
  72.         & "<Keystrokes> = Keystroke values as defined here:" & @CRLF _
  73.         & @TAB & "www.autoitscript.com/autoit3/docs/appendix/SendKeys.htm" & @CRLF)
  74.  
  75. Func writeLogLine($message)
  76. ; Write a line to the logfile, with a prefix of "yyyy-MM-dd hh:mm:ss.msec, ", as the generic _FileWriteLog function gave errors
  77.         Local $openLogFile = FileOpen($dbgfile,1)
  78.         If $openLogFile > -1 Then
  79.                 FileWriteLine($openLogFile,@YEAR&"-"&@MON&"-"&@MDAY&" "&@HOUR&":"&@MIN&":"&@SEC&"."&StringFormat("%03d",@MSEC)&", " & $message)
  80.                 FileClose($openLogFile)
  81.         EndIf


The compiled/non-upx'd exe is attached (updated: 2011-01-26)
« Last Edit: April 23, 2011, 10:55 AM by Ath, Reason: Re-routed to separate thread »

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • default avatar
  • Posts: 11,186
    • View Profile
    • Donate to Member
Re: Simple Commandline to send Keystrokes
« Reply #6 on: January 24, 2011, 09:29 PM »
Ath, I think we're not upx'ing them anymore because they're more likely to show up AV-positive if they are, so you might want to re-upload non-upx'd version.

See notification here.

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,610
    • View Profile
    • Donate to Member
Re: Simple Commandline to send Keystrokes
« Reply #7 on: January 25, 2011, 11:39 AM »
Ath, I think we're not upx'ing them anymore because they're more likely to show up AV-positive if they are, so you might want to re-upload non-upx'd version.

See notification here.
Oh, I missed/forgot that announcement, though I read a lot about upx'ed exe's being flagged by virusscanners :-[
Fixed it. And that's the second reason I published the source (first being this free-for-all), so anybody not trusting external exe's can re-compile it in a trusted environment.

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,610
    • View Profile
    • Donate to Member
Re: Simple Commandline to send Keystrokes
« Reply #8 on: January 25, 2011, 05:19 PM »
I updated the original script, to write to WinSendKeys.log when -d parameter is used, effectively re-enabling this option. The compiled attachment is ofcourse also updated.

Thanks a ton in advance!
This might have triggered me, even though I just yet read close enough to spot it :-*

Now let's wait for the OP to comment on this.
« Last Edit: January 25, 2011, 05:23 PM by Ath »

Split_e

  • Supporting Member
  • Joined in 2011
  • **
  • default avatar
  • Posts: 6
    • View Profile
    • Donate to Member
Re: Simple Commandline to send Keystrokes
« Reply #9 on: January 26, 2011, 01:30 PM »
Thanks so much guys! I have been following this and the other thread, and I just spent some time tinkering with it. Unfortunately, I'm having some issues. I'm pretty new to this stuff (I'm an engineering major, so my programming background is just VB6 and C++, some javascript). Can you tell me where I'm going wrong with this?

My "Tool Properties" window fields are:
Title: WinSendKeys
Program: C:\Users\Tyler\Books\Software\Ripping books\WinSendKeys.exe
Working Dir: C:\Users\Tyler\Books\Software\Ripping books\
Parameters: (blank)

I've tried a million variations of the commanline, but to be honest, I'm basically guessing. Of the many:
WinSendKeys <50> <"Materials"> <"{RIGHT}">
WinSendKeys [-t 50] [-d] [-w] <"Materials"> <"{RIGHT}">
WinSendKeys [-t 50] [-d] [-w] "Materials" "{RIGHT}"

The window is "Materials Science and Engineering: An Introduction, 8th Edition - CourseSmart Bookshelf". I've tried using the full name and different lengths, different times, different keys... :/

Any help is so very much appreciated!

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,610
    • View Profile
    • Donate to Member
Re: Simple Commandline to send Keystrokes
« Reply #10 on: January 27, 2011, 05:52 AM »
My "Tool Properties" window fields are:
Title: WinSendKeys
Program: C:\Users\Tyler\Books\Software\Ripping books\WinSendKeys.exe
Working Dir: C:\Users\Tyler\Books\Software\Ripping books\
Parameters: (blank)
That looks alright to me, now add the below stuff into 'Parameters:'

WinSendKeys <50> <"Materials"> <"{RIGHT}">
WinSendKeys [-t 50] [-d] [-w] <"Materials"> <"{RIGHT}">
WinSendKeys [-t 50] [-d] [-w] "Materials" "{RIGHT}"

The window is "Materials Science and Engineering: An Introduction, 8th Edition - CourseSmart Bookshelf". I've tried using the full name and different lengths, different times, different keys... :/
Hm, I've used the default documentation conventions for including optional parameters in square-brackets '[]' and 'explaining words' in angle-brackets '<>', and those shouldn't be actually included in the parameter list.
If you need to use a title with multiple words separated by spaces, then surround that with double-quotes.

So your parameters could be:
"- CourseSmart Bookshelf" {RIGHT}
To select the application CourseSmart Bookshelf with an opened document (the dash before CourseSmart indicates that), and send it a 'cursor right' keystroke, just like you pressed that key on the keyboard. If you want the PgDn key pressed, replace {RIGHT} with {PGDN}. You obviously found the list I referred. ;)
The timedelay should only be changed if your application can't handle the current speed of sending multiple keystrokes. It's only applied if the keystroke names/mnemonics are separated by spaces, so:
"{RIGHT} {RIGHT}" : Sends Right-Cursor, Space, Right-Cursor without any delay
{RIGHT} {RIGHT} : Sends Right-Cursor, (delay for 100 msec), Right-Cursor
You can (and probably should) experiment/test from a cmd-prompt.

Split_e

  • Supporting Member
  • Joined in 2011
  • **
  • default avatar
  • Posts: 6
    • View Profile
    • Donate to Member
Re: Simple Commandline to send Keystrokes
« Reply #11 on: January 27, 2011, 01:21 PM »
Awesome! Got it to work. Your explanation was very helpful Ath, but in the end, I needed to figure out what to put in:
"Post Capture Commandline Tools to Run - Invoke these command lines after each capture" Box on Screenshot Captor. I mistakenly thought that those commandlines called the Tools in the way they were configured with screen captor. Once I added the parameters to that line of code, everything worked great.

Unfortunately, the software I was using was updated yesterday to my dissapointment and they removed all keyboard shortcuts for changing the page. So I used AutoHotKey to send a mouse click to the particular button every time certain keystrokes were sent, then I used your tool to send those keys. Everything is working great! Thanks so much!

You've all been a huge help, with the quick replies and scripting. Thanks again!

Edit: I'm going to wait until I get paid so I can put in a bigger donation.
« Last Edit: January 28, 2011, 11:17 AM by Split_e »

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,610
    • View Profile
    • Donate to Member
Re: Simple Commandline to send Keystrokes
« Reply #12 on: January 28, 2011, 02:08 PM »
I've been fiddling with the script last night, and now it can also send mouse-clicks (so you won't need the separate ahk script for that). I'd call it v2.0 I guess :-\

I'll write down a readme, do some more testing, and wrap it all in one zip file for a little easier download, later tonight.

rjbull

  • Charter Member
  • Joined in 2005
  • ***
  • default avatar
  • Posts: 3,199
    • View Profile
    • Donate to Member
Re: Simple Commandline to send Keystrokes
« Reply #13 on: January 28, 2011, 02:47 PM »
I'll write down a readme, do some more testing, and wrap it all in one zip file
:Thmbsup:

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,610
    • View Profile
    • Donate to Member
Re: Simple Commandline to send Keystrokes
« Reply #14 on: January 28, 2011, 04:54 PM »
So, got the most stuff tested and corrected, and a readable readme done, so I can release WinSendKeys 2.0

Latest release & download moved to separate thread

Here's the Changelog for 2.0, copied from the script:
; 2011-01-27, version 2.0-beta,
; + Added mouse-modes, postfix with ## for ControlClick, #% for MouseClick
; + Added more logging and messages
; 2011-01-28, version 2.0,
; * Changed WinTitleMatchMode to be case-insensitive
; ! Fixed MouseClick mode
; + Allow Activate-Only by giving no keystrokes
; - Window not found message shows only in debug mode
; - Updated helpmessage

Hm, maybe Mouser could move/split this thread to the Coding Snacks/Finished section, as that's what it basically was  :-[

Attachment includes AutoIt3 source, readme and .exe
« Last Edit: April 23, 2011, 10:56 AM by Ath, Reason: Re-routed to separate thread »

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,610
    • View Profile
    • Donate to Member
Re: Simple Commandline to send Keystrokes
« Reply #15 on: January 30, 2011, 09:09 AM »
Another update and now WinSendKeys 2.5 is released

Latest release & download moved to separate thread

The changelog for 2.5 since the previous release, copied from the script:
; 2011-01-29, version 2.5-beta
; + Added -f strokesfile option to read keystrokes and mousestrokes from named file,
;            commandline strokes are handled first
; - Minor adjustments
; 2011-01-30, version 2.5
; ! Avoid ever overwriting the script with debug-log-file (if not .au3 or .exe extension, add .log to scriptname)
; + Added reading settings from optional .ini file:
;   Get parameters from .ini file with the same name as this script/exe, only if the file exists, create file manually!
;   Sectionname: [Settings]
;   Variables:
;   delay=100 : delay between keystrokes/mousestrokes sent to the application
;   mousestrokes=0 : Enable mousestrokes by setting this to 1
;   mousespeed=0 : Speedfactor for mouse move, value between 0 and 100, 0 = immediate, 1 = fast, 100 = slow
; - Documented and optimized script functions

Attachment includes AutoIt3 source, updated readme, a sample strokefile (.stro) and .exe
« Last Edit: April 23, 2011, 10:56 AM by Ath, Reason: Re-routed to separate thread »

asihuay

  • Participant
  • Joined in 2011
  • *
  • default avatar
  • Posts: 2
    • View Profile
    • Donate to Member
Re: Simple Commandline to send Keystrokes
« Reply #16 on: February 01, 2011, 06:44 PM »
Hi to all.
I need example of full command line to send keystrokes to an excel vba userform1
I am trying  the following:
UserForm1.Show vbModeless
Shell "S:\WinSendKeys.exe -w:UserForm1 Tab"   'try to send   Tab key to UserForm1
Shell "S:\WinSendKeys.exe -w:UserForm1 a"   'try to send   "a"   to UserForm1
thanks in advance.

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,610
    • View Profile
    • Donate to Member
Re: Simple Commandline to send Keystrokes
« Reply #17 on: February 02, 2011, 01:35 AM »
Please re-read the readme file, it has samples and explanations, and weblinks to the key mnemonics to use.

Your sample could look something like this, assuming the title of your form really is 'UserForm1' :
Shell "S:\WinSendKeys.exe -w UserForm1 {TAB} a"   'try to send Tab key &&  "a"  to UserForm1

But eventually, Excel vba offers enough methods to control that dialog directly, you don't need an external tool for that.
Probably someone with more vba experience can help out here?

asihuay

  • Participant
  • Joined in 2011
  • *
  • default avatar
  • Posts: 2
    • View Profile
    • Donate to Member
Re: Simple Commandline to send Keystrokes
« Reply #18 on: February 02, 2011, 02:46 PM »
Thanks for the code.
Yes for control userform with vba is very easy I use it only to example in order to obtain the correct command line code.
My need are to open in default browser all network camera in sequence all are protected by user and password at this point I will use winsendkeys and then run snapshot(alt+S) then enter to take picture.
At this time I successfully obtain the required snapshot.
Thanks.

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,610
    • View Profile
    • Donate to Member
Re: Simple Commandline to send Keystrokes
« Reply #19 on: February 07, 2011, 04:21 PM »
Another update and now WinSendKeys 2.6 is released.

Latest release & download moved to separate thread

The changelog for 2.6 since the previous 2.5 release:
; 2011-02-07, version 2.6
; + Added -c option to read keystrokes from the clipboard, as requested by mouser & kikon in this thread: https://www.donation...ndex.php?topic=25553
; + Added -cn option, like -c, but does not send an {ENTER} after each line, can be added after -c or -cc to disable the {Enter} being sent
; + Added -cc option, like -c but embeds the clipboard into the KeyStrokes commandline sequence using #$ prefix, see readme for details

Attachment includes AutoIt3 source, updated readme, a sample strokefile (.stro) and .exe

(Edit: typo fixed)
« Last Edit: April 23, 2011, 10:57 AM by Ath, Reason: Re-routed to separate thread »

kikon

  • Participant
  • Joined in 2010
  • *
  • default avatar
  • Posts: 19
    • View Profile
    • Donate to Member
Re: Simple Commandline to send Keystrokes
« Reply #20 on: February 08, 2011, 04:14 PM »
thanks a lot Ath !
I used this line :
WinSendKeys -w "Help and Support Center" -c

but what to do when application is not running. In most cases, we should send clipboard content to app that not running at the moment...

to use cmd.exe ? or you can add this feature to your utility ?
« Last Edit: February 08, 2011, 04:28 PM by kikon »

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,610
    • View Profile
    • Donate to Member
Re: Simple Commandline to send Keystrokes
« Reply #21 on: February 08, 2011, 04:48 PM »
I thought about that when working on this extension, but could not make a decision whether to add that as a feature or not. It's quite a different feature, as the executable of the file to load usually doesn't have much relation with the title of an active window. It would most likely require an extra commandline parameter. And while that's not really bad, it's imho a bit off of the original idea.

I'll think about it for a bit, and write here what I decide.

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,610
    • View Profile
    • Donate to Member
Re: Simple Commandline to send Keystrokes
« Reply #22 on: February 09, 2011, 01:40 PM »
I'll think about it for a bit, and write here what I decide.

Ok, I'll add a "-x" option, if it pleases you, that's fine (and fun) for me 8)

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,610
    • View Profile
    • Donate to Member
Re: Simple Commandline to send Keystrokes
« Reply #23 on: February 09, 2011, 05:58 PM »
Yet another update, and I've named it WinSendKeys 3.0 :-*

Latest release & download moved to separate thread

The excerpt from the changelog since 2.6, included in the source:
; 2011-02-09, version 3.0
; + Added -x executable_filepath option to start if WindowName is not found or executable filename is not found running
; + Added -xp executable_parameters option to specify parameters to executable_filepath

As usual, I updated the readme, compiled the AutoIt3 source into an exe, and pushed it all together in a zipfile, attached to this post.
« Last Edit: April 23, 2011, 10:57 AM by Ath, Reason: Re-routed to separate thread »

kikon

  • Participant
  • Joined in 2010
  • *
  • default avatar
  • Posts: 19
    • View Profile
    • Donate to Member
Re: Simple Commandline to send Keystrokes
« Reply #24 on: February 10, 2011, 05:21 PM »
thank you Ath, but -x doesn't work yet. I used in various way, doesn't work :(
d:\run\WinSendKeys\WinSendKeys.exe -x C:\WINDOWS\pchealth\helpctr\binaries\helpctr.exe -c

helpctr.exe just started and nothing is pasted.
« Last Edit: February 10, 2011, 05:38 PM by kikon »