WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Sending Keystrokes

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

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:

  1. SendKey - Sends keystrokes blindly to what it thinks is the active app.

  2. SendKeysTo - Sends Keystrokes to default active windows of an application.

  3. SendKeysChild - Sends Keystrokes to a particular child window of a particular application.

  4. 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.

  5. SendKey is the last resort, if SendKeysto or SendKeysChild don't work.
Additional notes:

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 SHIFTUP
I don't really think you do it that way. So try:

    SendKeysto("~Notepad", "! ep")

Article ID:   W13834
Filename:   Sending Keystrokes to Win and DOS apps.txt
File Created: 2001:01:31:10:38:16
Last Updated: 2001:01:31:10:38:16