Sending Keystrokes to Windows and DOS Applications
Keywords: sendkeys sendkeysto
Sending keystrokes and menus is sometimes a dicey situation. Each target application can choose to interpret keystrokes and mouse messages as it sees fit. Some software publishers don't see a need to re-invent the wheel, others do.Here are some pointers on sending keystrokes using WinBatch:
Additional notes:
- SendKey - Sends keystrokes blindly to what it thinks is the active app.
- SendKeysTo - Sends Keystrokes to default active windows of an application.
- SendKeysChild - Sends Keystrokes to a particular child window of a particular application.
- SendKeysChild is most preferrred when it works. If it does not work, or the application does not use child windows, then SendKeysTo is the next bet.
- SendKey is the last resort, if SendKeysto or SendKeysChild don't work.
- SendKeysChild only works with MDI applications. "MDI" is a specific structure any application can conform to.
- An alternative is to use:
MouseClickBtn(parent-windowname, child-windowname, button-text)which works quite well if you have a dialog window that has an underlined key letter in the dialog button text. You may have to fiddle with the child-windownames - perhaps set it to nul ("") to get it to work with some applications.
- Sendkey and Sendkeysto (the newer function) can only send keystrokes to windowed rather than to full screen mode windows.
To set your DOS program to a windowed rather than full-screen mode, set the _Default.pif file to run in a windowed state and close on exit. If the program has its own .pif file, make the changes in that .pif.
- Sendkey requires that the window be active, and so we have added the "Sendkeysto" function, which both activates the window and sends keystrokes to it.
- Because a window must be active before you can send keystrokes to it, you cannot send keystrokes to a hidden or iconized window.
- The Sendkeysto function is used to send keystrokes to the active window, just as if they had been entered from the keyboard. Any alphanumeric character, and most punctuation marks and other symbols which appear on the regular portion of the keyboard, may be sent.
In addition, the following special characters, enclosed in curly braces, may be sent:
Key SendKey equivalent ~ {~} ! {!} ^ {^} + {+} Alt {ALT} Backspace {BACKSPACE} or {BS} Caps Lock {CAPSLOCK} Clear {CLEAR} Delete {DELETE} or {DEL} Down Arrow {DOWN} End {END} Enter {ENTER} or ~ Escape {ESCAPE} or {ESC} F1 through F16 {F1} through {F16} 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} Space {SPACE} or {SP} Tab {TAB} Up Arrow {UP}
To enter an Alt, Control, or Shift key combination, precede the desired character with one or more of the following symbols: Alt ! Control ^ Shift + To enter Alt-S: SendKeysto("~Notepad", "!s")
Note: You should use lower-case letters to represent Alt-key combinations and other menu shortcut keys.
To enter Ctrl-Shift-F7:
SendKeysto("~Notepad", "^+{F7}")You may also repeat a key by enclosing it in braces, followed by a space and the total number of repetitions desired.
To type 20 asterisks: SendKeysto("~Notepad", "{* 20}")
To move the cursor down 8 lines: SendKeysto("~Notepad", "{DOWN 8}")It is possible to use SendKeysto to send keystrokes to a DOS application, but only if you are running Windows in 386 Enhanced mode. You would then transfer the keystrokes to the DOS application via the Clipboard. It is only possible to send standard ASCII characters to DOS applications; you cannot send function key or Alt-key combinations.Examples:; start Notepad, and use *.* for filenames Run("notepad.exe", "") SendKeysto("~Notepad", "!fo*.*~") ; run DOS batch file which starts our editor Run("edit.bat", "") ; wait 15 seconds for editor to load Delay(15) ; send string (with carriage return) to the clipboard crlf = StrCat(Num2Char(13), Num2Char(10)) ClipPut("Hello%crlf%") ; paste contents of clipboard to DOS window SendKeysto("~Notepad", "!{SP}ep")In those cases where you have an application which can accept text pasted in from the clipboard, it will often be more efficient to use the ClipGet function:
Run("notepad.exe", "") crlf = StrCat(Num2Char(13), Num2Char(10)) ; copy some text to the clipboard ClipPut("Dear Sirs:%crlf%%crlf%") ; paste the text into Notepad (using Shift-Ins) SendKeysto("~Notepad", "+{INSERT}")NOTE: A WIL program cannot send keystrokes to its own WIL Interpreter window.
If your SendKeysto statement doesn't seem to be working (e.g., all you get are beeping noises), you may need to recheck the name of the window to which you're trying to send keystrokes. In addition, if it's a Child window, see the function, SendkeysChild.
See Also:
ClipGet, SnapShot, WinActivate, SendkeysChild everything works perfectly except no sendkey! SendKeysto("~Notepad", "!(SP)EP")The stuff around the SP must be the special CURLY BRACES rather than the normal parenthesis.
More info:
SendKeysto("~Notepad", "!{SP}EP")Actually the following will also work:;when you just type a space for a space SendKeysto("~Notepad", "! EP")Also as a point of nitpicking:SendKeysto("~Notepad", "! EP") is the same as ALT SPACE SHIFTDOWN e p SHIFTUPI don't really think you do it that way. So try:SendKeysto("~Notepad", "! ep")
Article ID: W13834Filename: Sending Keystrokes to Win and DOS apps.txt