Keyboard API
- class autokey.scripting.Keyboard(mediator)[source]
-
Provides access to the keyboard for event generation.
- enum SendMode(value)
-
Enumeration class for phrase send modes
KEYBOARD: Send using key events CB_CTRL_V: Send via clipboard and paste with Ctrl+v CB_CTRL_SHIFT_V: Send via clipboard and paste with Ctrl+Shift+v SELECTION: Send via X selection and paste with middle mouse button
Valid values are as follows:
- KEYBOARD = <SendMode.KEYBOARD: 'kb'>
- CB_CTRL_V = <SendMode.CB_CTRL_V: '<ctrl>+v'>
- CB_CTRL_SHIFT_V = <SendMode.CB_CTRL_SHIFT_V: '<ctrl>+<shift>+v'>
- CB_SHIFT_INSERT = <SendMode.CB_SHIFT_INSERT: '<shift>+<insert>'>
- SELECTION = <SendMode.SELECTION: None>
- fake_keypress(key, repeat=1)[source]
Fake a keypress
Usage:
keyboard.fake_keypress(key, repeat=1)
Uses XTest to ‘fake’ a keypress. This is useful to send keypresses to some applications which won’t respond to keyboard.send_key()
- Parameters:
key – the key to be sent (e.g. “s” or “<enter>”)
repeat – number of times to repeat the key event
- press_key(key)[source]
Send a key down event
Usage:
keyboard.press_key(key)
The key will be treated as down until a matching release_key() is sent. :param key: the key to be pressed (e.g. “s” or “<enter>”)
- release_key(key)[source]
Send a key up event
Usage:
keyboard.release_key(key)
If the specified key was not made down using press_key(), the event will be ignored. :param key: the key to be released (e.g. “s” or “<enter>”)
- send_key(key, repeat=1)[source]
Send a keyboard event
Usage:
keyboard.send_key(key, repeat=1)
- Parameters:
key – the key to be sent (e.g. “s” or “<enter>”)
repeat – number of times to repeat the key event
- send_keys(key_string, send_mode: SendMode | int = SendMode.KEYBOARD)[source]
Send a sequence of keys via keyboard events as the default or via clipboard pasting. Because the clipboard can only contain printable characters, special keys and embedded key combinations can only be sent in keyboard mode.
Trying to send special keys using a clipboard pasting method will paste the literal representation (e.g. “<ctrl>+<f11>”) instead of the actual special key or key combination.
Usage:
keyboard.send_keys(keyString)
- Parameters:
key_string – string of keys to send. Special keys are only possible in keyboard mode.
send_mode – Determines how the string is sent.
- wait_for_keyevent(check: Callable[[any, str, list, str], bool], name: str | None = None, timeOut=10.0)[source]
Wait for a key event, potentially accumulating the intervening characters Usage:
keyboard.wait_for_keypress(self, check, name=None, timeOut=10.0)
:param check: a function that returns True or False to signify we’ve finished waiting :param name: only one waiter can have this name. Used to prevent more threads waiting on this. :param timeOut: maximum time, in seconds, to wait for the keypress to occur Example: # Accumulate the traditional emacs C-u prefix arguments # See https://www.gnu.org/software/emacs/manual/html_node/elisp/Prefix-Command-Arguments.html def check(waiter,rawKey,modifiers,key,*args):isCtrlU = (key == ‘u’ and len(modifiers) == 1 and modifiers[0] == ‘<ctrl>’) if isCtrlU: # If we get here, they’ve already pressed C-u at least 2x
- try:
val = int(waiter.result) * 4 waiter.result = str(val)
- except ValueError:
waiter.result = “16”
return False
- elif any(m == “<ctrl>” or m == “<alt>” or m == “<meta>” or m == “<super>” or m == “<hyper>” for m in modifiers):
# Some other control character is an indication we’re done. if waiter.result is None or waiter.result == “”:
waiter.result = “4”
store.set_global_value(“emacs-prefix-arg”, waiter.result) return True
- else: # accumulate as a string
waiter.result = waiter.result + key return False
keyboard.wait_for_keyevent(check, “emacs-prefix”)
- wait_for_keypress(key, modifiers: list | None = None, timeOut=10.0)[source]
Wait for a keypress or key combination
Usage:
keyboard.wait_for_keypress(self, key, modifiers=[], timeOut=10.0)
Note: this function cannot be used to wait for modifier keys on their own
- Parameters:
key – the key to wait for
modifiers – list of modifiers that should be pressed with the key
timeOut – maximum time, in seconds, to wait for the keypress to occur
Keys
- enum autokey.model.key.Key(value)[source]
An enumeration.
- Member Type:
str
Valid values are as follows:
- LEFT = <Key.LEFT: '<left>'>
- RIGHT = <Key.RIGHT: '<right>'>
- UP = <Key.UP: '<up>'>
- DOWN = <Key.DOWN: '<down>'>
- BACKSPACE = <Key.BACKSPACE: '<backspace>'>
- TAB = <Key.TAB: '<tab>'>
- ENTER = <Key.ENTER: '<enter>'>
- SCROLL_LOCK = <Key.SCROLL_LOCK: '<scroll_lock>'>
- PRINT_SCREEN = <Key.PRINT_SCREEN: '<print_screen>'>
- PAUSE = <Key.PAUSE: '<pause>'>
- MENU = <Key.MENU: '<menu>'>
- CONTROL = <Key.CONTROL: '<ctrl>'>
- ALT = <Key.ALT: '<alt>'>
- ALT_GR = <Key.ALT_GR: '<alt_gr>'>
- SHIFT = <Key.SHIFT: '<shift>'>
- SUPER = <Key.SUPER: '<super>'>
- HYPER = <Key.HYPER: '<hyper>'>
- CAPSLOCK = <Key.CAPSLOCK: '<capslock>'>
- NUMLOCK = <Key.NUMLOCK: '<numlock>'>
- META = <Key.META: '<meta>'>
- F1 = <Key.F1: '<f1>'>
- F2 = <Key.F2: '<f2>'>
- F3 = <Key.F3: '<f3>'>
- F4 = <Key.F4: '<f4>'>
- F5 = <Key.F5: '<f5>'>
- F6 = <Key.F6: '<f6>'>
- F7 = <Key.F7: '<f7>'>
- F8 = <Key.F8: '<f8>'>
- F9 = <Key.F9: '<f9>'>
- F10 = <Key.F10: '<f10>'>
- F11 = <Key.F11: '<f11>'>
- F12 = <Key.F12: '<f12>'>
- F13 = <Key.F13: '<f13>'>
- F14 = <Key.F14: '<f14>'>
- F15 = <Key.F15: '<f15>'>
- F16 = <Key.F16: '<f16>'>
- F17 = <Key.F17: '<f17>'>
- F18 = <Key.F18: '<f18>'>
- F19 = <Key.F19: '<f19>'>
- F20 = <Key.F20: '<f20>'>
- F21 = <Key.F21: '<f21>'>
- F22 = <Key.F22: '<f22>'>
- F23 = <Key.F23: '<f23>'>
- F24 = <Key.F24: '<f24>'>
- F25 = <Key.F25: '<f25>'>
- F26 = <Key.F26: '<f26>'>
- F27 = <Key.F27: '<f27>'>
- F28 = <Key.F28: '<f28>'>
- F29 = <Key.F29: '<f29>'>
- F30 = <Key.F30: '<f30>'>
- F31 = <Key.F31: '<f31>'>
- F32 = <Key.F32: '<f32>'>
- F33 = <Key.F33: '<f33>'>
- F34 = <Key.F34: '<f34>'>
- F35 = <Key.F35: '<f35>'>
- ESCAPE = <Key.ESCAPE: '<escape>'>
- INSERT = <Key.INSERT: '<insert>'>
- DELETE = <Key.DELETE: '<delete>'>
- HOME = <Key.HOME: '<home>'>
- END = <Key.END: '<end>'>
- PAGE_UP = <Key.PAGE_UP: '<page_up>'>
- PAGE_DOWN = <Key.PAGE_DOWN: '<page_down>'>
- NP_INSERT = <Key.NP_INSERT: '<np_insert>'>
- NP_DELETE = <Key.NP_DELETE: '<np_delete>'>
- NP_HOME = <Key.NP_HOME: '<np_home>'>
- NP_END = <Key.NP_END: '<np_end>'>
- NP_PAGE_UP = <Key.NP_PAGE_UP: '<np_page_up>'>
- NP_PAGE_DOWN = <Key.NP_PAGE_DOWN: '<np_page_down>'>
- NP_LEFT = <Key.NP_LEFT: '<np_left>'>
- NP_RIGHT = <Key.NP_RIGHT: '<np_right>'>
- NP_UP = <Key.NP_UP: '<np_up>'>
- NP_DOWN = <Key.NP_DOWN: '<np_down>'>
- NP_DIVIDE = <Key.NP_DIVIDE: '<np_divide>'>
- NP_MULTIPLY = <Key.NP_MULTIPLY: '<np_multiply>'>
- NP_ADD = <Key.NP_ADD: '<np_add>'>
- NP_SUBTRACT = <Key.NP_SUBTRACT: '<np_subtract>'>
- NP_5 = <Key.NP_5: '<np_5>'>
The
Enum
and its members also have the following methods: