-
Notifications
You must be signed in to change notification settings - Fork 225
Key Combinations
Keys and key combinations can be pressed for you automatically by AutoKey in a variety of ways, with the methods below being built in for your convenience.
You can insert any number of key combinations into phrases. The keys for the letters, numbers, and characters in any plain text will be pressed without any modification. AutoKey macros can be used to modify the key-presses or to add special characters not found on the keyboard.
example<backspace>This line will send some text followed by a backspace followed by this text.
<delete>This line will send a delete followed by this text.
This line will send this text followed by the number seven from the numpad.<numlock>+<code79>
This line will send this text followed by a vulgar one-tenth fraction. <ctrl>+<shift>+u+2152
<shift>+this line will start with an upper-case t because the shift key was pressed with the t.
This line will send this text. The next line will press the specified keys, but the key-combination will only work in the focused window and not globally.
<shift><ctrl>+o
The keyboard.send_key() method accepts only one key.
keyboard.send_key("<tab>")
The keyboard.fake_keypress() method accepts only one key. This method uses XTest to "fake" a key-press and can be useful for sending a key-press if an application doesn't respond to the keyboard.send_key() method.
keyboard.fake_keypress("<up>", repeat=3)
The keyboard.send_keys() method accepts any number of keys. When sending multiple keys, a plus sign between keys causes the keys on either side of it to be pressed at the same time. Leaving off the plus sign between keys causes them to be pressed and released one after another. With a combination of both approaches in the same statement, you can achieve a variety of key-press combinations.
keyboard.send_keys("<tab>")
keyboard.send_keys("ab")
keyboard.send_keys("<shift>+a")
keyboard.send_keys("<shift>+at")
keyboard.send_keys("<ctrl>+<alt>pb")
keyboard.send_keys("<shift>+apple")
The keyboard.press_key() method presses and holds a key down until it's released by the keyboard.release_key() method. Note that a pressed key will remain pressed even after AutoKey is closed if it wasn't released as part of your script.
keyboard.press_key("<tab>")
keyboard.release_key("<tab>")
keyboard.press_key("a")
keyboard.release_key("a")
keyboard.press_key("b")
keyboard.release_key("b")
keyboard.press_key("<shift>")
keyboard.press_key("a")
keyboard.release_key("a")
keyboard.release_key("<shift>")
keyboard.press_key("<shift>")
keyboard.press_key("a")
keyboard.release_key("a")
keyboard.release_key("<shift>")
keyboard.press_key("t")
keyboard.release_key("t")
(NOTE: This is not possible with the other methods.)
keyboard.press_key("<shift>")
keyboard.press_key("a")
keyboard.release_key("a")
keyboard.press_key("b")
keyboard.release_key("b")
keyboard.press_key("c")
keyboard.release_key("c")
keyboard.release_key("<shift>")
keyboard.press_key("<shift>")
keyboard.press_key("a")
keyboard.release_key("a")
keyboard.release_key("<shift>")
keyboard.press_key("p")
keyboard.release_key("p")
keyboard.press_key("p")
keyboard.release_key("p")
keyboard.press_key("l")
keyboard.release_key("l")
keyboard.press_key("e")
keyboard.release_key("e")
The keyboard.send_key() and keyboard.send_keys() API calls affect what's sent in different ways depending on the type of key that's sent.
- Effect on double-value keys:
- The
keyboard.send_key()API call:- doesn't press the Shift key when default key values are sent
- doesn't press the Shift key when alternate (shifted) key values are sent.
- The
keyboard.send_keys()API call- doesn't press the Shift key when default key values are sent.
- does press the Shift key when alternate (shifted) key values are sent.
- Examples:
-
keyboard.send_key("*")sends 8 -
keyboard.send_key("8")sends 8 -
keyboard.send_keys("*")sends * -
keyboard.send_keys("8")sends 8
-
- The
- Effect on single-value keys:
- The
keyboard.send_key()API call:- sends letters in lower-case even if an upper-case letter is sent or the Caps Lock key is enabled or the Shift key is held down while the script is triggered.
- The
keyboard.send_keys()API call:- sends letters as typed
- intelligently handles mixed cases
- Examples:
-
keyboard.send_key("a")sends a -
keyboard.send_key("A")sends a -
keyboard.send_keys("a")sends a -
keyboard.send_keys("A")sends A -
keyboard.send_keys("ABC")sends ABC -
keyboard.send_keys("Abc")sends Abc
-
- The
In Linux, you can run the xev command in a terminal window and then press any key(s) to get their their keycode(s). Mac and Windows may offer something similar.
See also the Emitting Keyboard Events page, which provides some related information not included here.
-
Home
- About
- Beginners' Guide
- Documentation
- FAQ
- Administration
- Community
- Development
- Features
- Development Testing