Home > Language Reference > Windows Management Functions > Sendkeys

Sendkeys

The SendKeys function sends the specified keystrokes to the current window.

Usage

Used to simulate keystrokes being typed into an application. For example, the first line in a script could be to open an application using Shell function, the next step would be a SendKeys to send keystrokes. Before the SendKeys line, a UseWindow statement would specify the window where the keystrokes have to be sent.

Syntax

ret=SendKeys(<key_list> [, Noactivate])

Parameters

<key_list>, string, keystrokes to send. Case is preserved. Special keystrokes (Alt, Shift, ...) must be specified using their keyboard mnemonics.

Noactivate, optional keyword. It is used for sending keystrokes to the window which has the focus without checking that it is the one defined in the most recent UseWindow. For example, with Noactivate option, you can send keystrokes to different fields in a form without a UseWindow line identifying each field window name ; you can simulate a move from one field to another using:
SendKeys("<Tab>",Noactivate)

Return value

ret, optional numeric return value. If the function succeeds, the return value is 0. If the function fails, the return value is a nonzero value (see Window Functions Error Codes).

Remarks

The system variable #SendKeysDelay (set to 0 by default) slows down the typing of characters. A small delay is added between each keystroke (for example, if SendKeys sends "z<Ctrl a>", there will be a delay between z  Ctrl and a).

SendKeys function cannot send as is keyboard mnemonics such as <Alt <Enter>>, you need to use the keyboard mnemonics for the string to send, "normal text"+"<Alt <Enter>>"+"normal text".

For sending text to a field specified by its UIA descriptor, use WriteUIA function.

See also

SendKeysEncrypted
WriteUIA

Examples

'Using Recording mode, you can generate the Notepad launch and some typing into notepad
Shell("notepad")
UseWindow("NOTEPAD.EXE|Edit|Untitled - Notepad|1",1)
SendKeys("Hello")
SendKeys("<Enter>")
SendKeys("Hello<Enter><Alt <F5>>")

'Variables can be used:
UseWindow("NOTEPAD.EXE|Edit|Untitled - Notepad|1",1)
a$="Hello"
SendKeys(a$)