Skip to content

Keyboard functions#

Functions to type text and press keys, for when a macro needs to fill in a form or use keyboard shortcuts on the page.

Keys and text go to the element that has focus, so click the input field first. For the K_* key codes and the WITH_* combinations, see Key constants.

Typing#

Types a string into the element that has focus, one character at a time, like a person typing. The function does not set focus itself: if you have not clicked the input field, the text lands somewhere else.

Syntax
await Typing(text, speed1=0, speed2=0);
Parameters
Name Description
text The string to type.
speed1 Shortest pause between two characters, in milliseconds. Default 0.
speed2 Longest pause between two characters, in milliseconds; each pause is a random length between speed1 and speed2. If it is smaller than speed1, every pause is exactly speed1. Default 0.
Examples
1
2
3
4
5
6
7
// click the email field, wait a moment, then type with a 60-140 ms pause between characters
await ClickBySelector('input[name="email"]');
await Delay(1000);
await Typing("[email protected]", 60, 140);

// type more slowly, with 300-500 ms pauses
await Typing("Hello 9Hits", 300, 500);

Note

If both speed1 and speed2 are 0, the whole string is typed in one burst, which does not look like a person typing. Set a pause of a few tens to a few hundred milliseconds.

SendKeyPress#

Presses and releases a key on the element that has focus, for example Enter to submit a form or Ctrl + V to paste.

Syntax
await SendKeyPress(keyCode, modifiers=0);
Parameters
Name Description
keyCode Key code: a K_* constant, for example K_ENTER, K_KEYA (see Key codes). It must be a number: if you pass a string such as "a", the function presses nothing. To send a character, use SendKeyChar or Typing.
modifiers Keys held down at the same time: add up the constants WITH_ALT, WITH_CTRL, WITH_COMMAND, WITH_SHIFT (see Key constants). Default 0.
Examples
1
2
3
await SendKeyPress(K_ENTER);                        // press Enter
await SendKeyPress(K_KEYV, WITH_CTRL);              // Ctrl + V
await SendKeyPress(K_KEYV, WITH_CTRL | WITH_SHIFT); // Ctrl + Shift + V

SendKeyDown#

Presses and holds a key without releasing it. Release the key with SendKeyUp, using the same keyCode and modifiers.

Syntax
await SendKeyDown(keyCode, modifiers=0);
Parameters
Name Description
keyCode Key code: a K_* constant, for example K_KEYA (see Key codes). It must be a number; if you pass a string, the function does nothing.
modifiers Keys held down at the same time: add up the constants WITH_ALT, WITH_CTRL, WITH_COMMAND, WITH_SHIFT (see Key constants). Default 0.
Example
1
2
3
// Ctrl + A: select everything in the focused field
await SendKeyDown(K_KEYA, WITH_CTRL);
await SendKeyUp(K_KEYA, WITH_CTRL);

SendKeyUp#

Releases a key, usually after SendKeyDown.

Syntax
await SendKeyUp(keyCode, modifiers=0);
Parameters
Name Description
keyCode Key code: a K_* constant, for example K_KEYA (see Key codes). It must be a number; if you pass a string, the function does nothing.
modifiers Keys held down at the same time: add up the constants WITH_ALT, WITH_CTRL, WITH_COMMAND, WITH_SHIFT (see Key constants). Default 0.
Example
1
2
3
4
// hold A down, then release it after half a second
await SendKeyDown(K_KEYA);
await Delay(500);
await SendKeyUp(K_KEYA);

SendKeyChar#

Sends one character to the element that has focus, as if typed, without key down and key up events. To type a whole string, use Typing.

Syntax
await SendKeyChar(character, modifiers=0);
Parameters
Name Description
character One character, for example "a", "@", "é"; or a numeric character code, for example 97 for "a".
modifiers Keys held down at the same time: add up the constants WITH_ALT, WITH_CTRL, WITH_COMMAND, WITH_SHIFT (see Key constants). Default 0.
Examples
await SendKeyChar("a"); // types "a"
await SendKeyChar(97);  // also types "a", by character code