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.
- Use uppercase letters for non-text keys, such as Ctrl, Enter, F2, and numeric keypad keys.
- Example: key TT Y F2 N3 ;;Tab Tab Enter F2 Num3.
- Use lowercace letters and other characters for text keys.
- Example: key a/2 ;;A / 2.
- Characters ;"{}() have special meaning. Instead use
:'[]90 or enclose in double quotes.
- Add modifier keys (Ctrl, Shift, Alt, Win) before the modified key.
- Example: key Wf CSF2 ;;Win+F Ctrl+Shift+F2.
- If several keys, enclose in {}. Example: key A{ev} ;;Alt+E+V.
2. Text in double quotes. Sends keys that type the text.
- Example: key "Text" ;;types Text (sends Shift+T E X T).
- Can be used escape sequences, like in all strings. Example: key "a[][9]b" ;;A Enter Tab B.
- Can be F-string. Example: int x=5; key F"x={x}" ;;x=5.
- The key messages received by the target window depend on opt keychar.
3. String expression (e.g. variable) in parentheses. Types text, like in case 2.
- Example: str s="Text[]more text"; key (s).
- To type a non-string variable, convert it to string. Example: int x=5; str s=x; key (s). Or use F-string, see above.
4. Windows virtual-key code in parentheses. Sends the key.
- Example: key (VK_VOLUME_UP).
- Example: int vk='A'; key (vk).
5. Scan code in parentheses. Sends the key.
- Use flag 0x10000. If it is an extended key, add flag 0x20000.
- Example: key (0x30000|0x38) ;;Right Alt (scan code 0x38, extended key).
6. Unicode character code in parentheses. Types the character.
- Use flag 0x40000.
- Example: key (0x3A3|0x40000) ;;Σ.
- In Unicode mode you can instead simply use Unicode characters in text
enclosed in double quotes (case 2).
7. Double (floating-point) expression in parentheses inserts a delay.
- Example: key a (1.0) b ;;A, wait 1 s, B.
- Example: double d=0.5; key a (d) b.
8 (QM 2.2.1). Flags in parentheses.
- 0x01000000 - same as option +.
- 0x02000000 - same as option -.
- 0x03000000 - removes the above flags and options + -.
9 (QM 2.3.3). Key repeat count in parentheses, with # prefix.
- Example: key T(#10) ;;press Tab 10 times.
- Example: int i=5; key a(#i) ;;press A i times.
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, admin
apps on Vista/7/8/10
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