Send keys and text

Syntax1

key[+|-] keys

 

Syntax2

'keys

 

Remarks

Generates (synthesizes, simulates) keyboard events. It works like you would press keyboard keys manually. The key events will be sent to the focused window or to the application that uses them as a global hotkey.

 

To create code for key, you can use dialogs 'Text' and 'Keys'.

 

keys can include any number of parts of these types:

 

1. Parts without quotes and parentheses. Sends keys specified using QM key codes.

 

2. Text in double quotes. Sends keys that type the text.

 

3. String expression (e.g. variable) in parentheses. Types text, like in case 2.

 

4. Windows virtual-key code in parentheses. Sends the key.

 

5. Scan code in parentheses. Sends the key.

 

6. Unicode character code in parentheses. Types the character.

 

7. Double (floating-point) expression in parentheses inserts a delay.

 

8 (QM 2.2.1). Flags in parentheses.

 

9 (QM 2.3.3). Key repeat count in parentheses, with # prefix.

 

Options:

+ press keys down but don't release.
- release keys, don't press.

 

Options and flags 0x01000000 and 0x02000000 can be used in case 1, 4 and 5.

 

Syntax2 can be used to make macro smaller.

 

The speed depends on spe and opt slowkeys.

 

QM 2.2.1: key can be used as function. Then it does not send keys, but instead returns virtual-key codes. Read more

If used as function, key returns ARRAY(KEYEVENT). Definition of KEYEVENT:

 

KEYEVENT !vk !flags @sc [0]wt

 

vk - virtual-key code.

flags - KEYEVENTF_x flags. Also can be 0x80, which says that next element contains wait time (see below).

sc - scan code, or UNICODE character, depending on flags.

wt - wait time in milliseconds. A delay is specified using two elements. First element contains nothing more than flag 0x80. Next element contains nothing more than wait time. Note that wt is in union with other members.

 

All this info (except delay) is prepared to use with SendInput. Also can be used with some other Windows API functions, for example keybd_event, PostMessage(WM_KEYDOWN/WM_KEYUP).

 

It is convenient to use key as an argument of a function that accepts ARRAY(KEYEVENT). Example:

 

KeyPostToControl key(abc) id(15 "Notepad")

 

Function KeyPostToControl:

 /
function ARRAY(KEYEVENT)'a hwnd
...

 

 

Tips

You can use function BlockInput to block keyboard and mouse input while macro is running. If you want to manually end macro when input is blocked, at first press Ctrl+Alt+Delete.

 

When not using low level keyboard hook, if the user presses keys simultaneously with the key command, user-pressed keys may be inserted between key-sent keys. To avoid it, use opt keymark 1.

 

See also: Windows keyboard shortcuts, opt slowkeys/keysync/hungwindow, escaping special characters, UAC

 

Examples

key ab c F12 T Y      ;;A, B, C, F12, Tab, Enter
key Ca                ;;Ctrl+A
key CSf A{ec} Wd      ;;Ctrl+Shift+F, Alt+E+C, Win+D
key "Send Keys"       ;;type "Send Keys"
key SsendVSkeys       ;;type "Send Keys" using QM key codes
key (s)               ;;type string variable s
key F"{x}"            ;;type non-string variable x
key (VK_F2)           ;;F2 (use virtual-key code constant)
key A(44)             ;;Alt+PrintScreen (use virtual-key code)
key a (0.5) b         ;;A, wait 0.5 s, B
key Skeys "Keys" A(44) AT (0.5) Cv A{fa} Y
'AT                   ;;Alt+Tab (same as key AT)

 Ctrl+Shift+click:
key+ CS
lef
key- SC