keyb__init__.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. # -*- coding: utf-8 -*-
  2. """
  3. keyboard
  4. ========
  5. Take full control of your keyboard with this small Python library. Hook global events, register hotkeys, simulate key presses and much more.
  6. ## Features
  7. - Global event hook on all keyboards (captures keys regardless of focus).
  8. - **Listen** and **sends** keyboard events.
  9. - Works with **Windows** and **Linux** (requires sudo), with experimental **OS X** support (thanks @glitchassassin!).
  10. - **Pure Python**, no C modules to be compiled.
  11. - **Zero dependencies**. Trivial to install and deploy, just copy the files.
  12. - **Python 2 and 3**.
  13. - Complex hotkey support (e.g. `Ctrl+Shift+M, Ctrl+Space`) with controllable timeout.
  14. - Includes **high level API** (e.g. [record](#keyboard.record) and [play](#keyboard.play), [add_abbreviation](#keyboard.add_abbreviation)).
  15. - Maps keys as they actually are in your layout, with **full internationalization support** (e.g. `Ctrl+ç`).
  16. - Events automatically captured in separate thread, doesn't block main program.
  17. - Tested and documented.
  18. - Doesn't break accented dead keys (I'm looking at you, pyHook).
  19. - Mouse support available via project [mouse](https://github.com/boppreh/mouse) (`pip install mouse`).
  20. This program makes no attempt to hide itself, so don't use it for keyloggers.
  21. ## Usage
  22. Install the [PyPI package](https://pypi.python.org/pypi/keyboard/):
  23. $ sudo pip install keyboard
  24. or clone the repository (no installation required, source files are sufficient):
  25. $ git clone https://github.com/boppreh/keyboard
  26. Then check the [API docs](https://github.com/boppreh/keyboard#api) to see what features are available.
  27. ## Example
  28. ```
  29. import keyboard
  30. keyboard.press_and_release('shift+s, space')
  31. keyboard.write('The quick brown fox jumps over the lazy dog.')
  32. # Press PAGE UP then PAGE DOWN to type "foobar".
  33. keyboard.add_hotkey('page up, page down', lambda: keyboard.write('foobar'))
  34. # Blocks until you press esc.
  35. keyboard.wait('esc')
  36. # Record events until 'esc' is pressed.
  37. recorded = keyboard.record(until='esc')
  38. # Then replay back at three times the speed.
  39. keyboard.play(recorded, speed_factor=3)
  40. # Type @@ then press space to replace with abbreviation.
  41. keyboard.add_abbreviation('@@', 'my.long.email@example.com')
  42. # Block forever.
  43. keyboard.wait()
  44. ```
  45. ## Known limitations:
  46. - Events generated under Windows don't report device id (`event.device == None`). [#21](https://github.com/boppreh/keyboard/issues/21)
  47. - Linux doesn't seem to report media keys. [#20](https://github.com/boppreh/keyboard/issues/20)
  48. - Currently no way to suppress keys ('catch' events and block them). [#22](https://github.com/boppreh/keyboard/issues/22)
  49. - To avoid depending on X the Linux parts reads raw device files (`/dev/input/input*`)
  50. but this requries root.
  51. - Other applications, such as some games, may register hooks that swallow all
  52. key events. In this case `keyboard` will be unable to report events.
  53. """
  54. import time as _time
  55. from threading import Thread as _Thread
  56. from ._keyboard_event import KeyboardEvent
  57. from ._suppress import KeyTable as _KeyTable
  58. try:
  59. # Python2
  60. long, basestring
  61. is_str = lambda x: isinstance(x, basestring)
  62. is_number = lambda x: isinstance(x, (int, long))
  63. import Queue as queue
  64. except NameError:
  65. # Python3
  66. is_str = lambda x: isinstance(x, str)
  67. is_number = lambda x: isinstance(x, int)
  68. import queue
  69. # Just a dynamic object to store attributes for the closures.
  70. class _State(object): pass
  71. import platform as _platform
  72. if _platform.system() == 'Windows':
  73. from. import _winkeyboard as _os_keyboard
  74. elif _platform.system() == 'Linux':
  75. from. import _nixkeyboard as _os_keyboard
  76. elif _platform.system() == 'Darwin':
  77. from. import _darwinkeyboard as _os_keyboard
  78. else:
  79. raise OSError("Unsupported platform '{}'".format(_platform.system()))
  80. from ._keyboard_event import KEY_DOWN, KEY_UP
  81. from ._keyboard_event import normalize_name as _normalize_name
  82. from ._generic import GenericListener as _GenericListener
  83. all_modifiers = ('alt', 'alt gr', 'ctrl', 'shift', 'win')
  84. _pressed_events = {}
  85. class _KeyboardListener(_GenericListener):
  86. def init(self):
  87. _os_keyboard.init()
  88. def pre_process_event(self, event):
  89. if not event.scan_code and event.name == 'unknown':
  90. return False
  91. # Useful for media keys, which are reported with scan_code = 0.
  92. if not event.scan_code:
  93. event.scan_code = to_scan_code(event.name)
  94. if event.event_type == KEY_UP:
  95. if event.scan_code in _pressed_events:
  96. del _pressed_events[event.scan_code]
  97. else:
  98. _pressed_events[event.scan_code] = event
  99. if not _pressed_events:
  100. _key_table.complete_sequence()
  101. return True
  102. def listen(self):
  103. _os_keyboard.listen(self.queue, _key_table.is_allowed)
  104. _listener = _KeyboardListener()
  105. def matches(event, name):
  106. """
  107. Returns True if the given event represents the same key as the one given in
  108. `name`.
  109. """
  110. if is_number(name):
  111. return event.scan_code == name
  112. normalized = _normalize_name(name)
  113. matched_name = (
  114. normalized == event.name
  115. or 'left ' + normalized == event.name
  116. or 'right ' + normalized == event.name
  117. )
  118. return matched_name or _os_keyboard.map_char(normalized)[0] == event.scan_code
  119. def is_pressed(key):
  120. """
  121. Returns True if the key is pressed.
  122. is_pressed(57) -> True
  123. is_pressed('space') -> True
  124. is_pressed('ctrl+space') -> True
  125. """
  126. _listener.start_if_necessary()
  127. if is_number(key):
  128. return key in _pressed_events
  129. elif len(key) > 1 and ('+' in key or ',' in key):
  130. parts = canonicalize(key)
  131. if len(parts) > 1:
  132. raise ValueError('Cannot check status of multi-step combination ({}).'.format(key))
  133. return all(is_pressed(part) for part in parts[0])
  134. else:
  135. for event in _pressed_events.values():
  136. if matches(event, key):
  137. return True
  138. return False
  139. def canonicalize(hotkey):
  140. """
  141. Splits a user provided hotkey into a list of steps, each one made of a list
  142. of scan codes or names. Used to normalize input at the API boundary. When a
  143. combo is given (e.g. 'ctrl + a, b') spaces are ignored.
  144. canonicalize(57) -> [[57]]
  145. canonicalize([[57]]) -> [[57]]
  146. canonicalize('space') -> [['space']]
  147. canonicalize('ctrl+space') -> [['ctrl', 'space']]
  148. canonicalize('ctrl+space, space') -> [['ctrl', 'space'], ['space']]
  149. Note we must not convert names into scan codes because a name may represent
  150. more than one physical key (e.g. two 'ctrl' keys).
  151. """
  152. if isinstance(hotkey, list) and all(isinstance(step, list) for step in hotkey):
  153. # Already canonicalized, nothing to do.
  154. return hotkey
  155. elif is_number(hotkey):
  156. return [[hotkey]]
  157. if not is_str(hotkey):
  158. raise ValueError('Unexpected hotkey: {}. Expected int scan code, str key combination or normalized hotkey.'.format(hotkey))
  159. if len(hotkey) == 1 or ('+' not in hotkey and ',' not in hotkey):
  160. return [[_normalize_name(hotkey)]]
  161. else:
  162. steps = []
  163. for str_step in hotkey.split(','):
  164. steps.append([])
  165. for part in str_step.split('+'):
  166. steps[-1].append(_normalize_name(part.strip()))
  167. return steps
  168. def call_later(fn, args=(), delay=0.001):
  169. """
  170. Calls the provided function in a new thread after waiting some time.
  171. Useful for giving the system some time to process an event, without blocking
  172. the current execution flow.
  173. """
  174. _Thread(target=lambda: _time.sleep(delay) or fn(*args)).start()
  175. def _suppress_hotkey(steps, timeout):
  176. """
  177. Adds a hotkey to the list of keys to be suppressed.
  178. To unsuppress all hotkeys use `clear_all_hotkeys()`.
  179. """
  180. _key_table.suppress_sequence(steps, timeout)
  181. _hotkeys = {}
  182. _hotkeys_suppressed = {}
  183. def clear_all_hotkeys():
  184. """
  185. Removes all hotkey handlers. Note some functions such as 'wait' and 'record'
  186. internally use hotkeys and will be affected by this call.
  187. Abbreviations and word listeners are not hotkeys and therefore not affected.
  188. To remove all hooks use `unhook_all()`.
  189. """
  190. for handler in _hotkeys.values():
  191. unhook(handler)
  192. _hotkeys.clear()
  193. _key_table.suppress_none()
  194. _hotkeys_suppressed.clear()
  195. # Alias.
  196. remove_all_hotkeys = clear_all_hotkeys
  197. def add_hotkey(hotkey, callback, args=(), suppress=False, min_time=0.09, timeout=1, trigger_on_release=False):
  198. """
  199. Invokes a callback every time a key combination is pressed. The hotkey must
  200. be in the format "ctrl+shift+a, s". This would trigger when the user holds
  201. ctrl, shift and "a" at once, releases, and then presses "s". To represent
  202. literal commas, pluses and spaces use their names ('comma', 'plus',
  203. 'space').
  204. - `args` is an optional list of arguments to passed to the callback during
  205. each invocation.
  206. - `suppress` defines if the it should block processing other hotkeys after
  207. a match is found. Currently Windows-only.
  208. - `timeout` is the amount of seconds allowed to pass between key presses.
  209. - `trigger_on_release` if true, the callback is invoked on key release instead
  210. of key press.
  211. The event handler function is returned. To remove a hotkey call
  212. `remove_hotkey(hotkey)` or `remove_hotkey(handler)`.
  213. before the combination state is reset.
  214. Note: hotkeys are activated when the last key is *pressed*, not released.
  215. Note: the callback is executed in a separate thread, asynchronously. For an
  216. example of how to use a callback synchronously, see `wait`.
  217. add_hotkey(57, print, args=['space was pressed'])
  218. add_hotkey(' ', print, args=['space was pressed'])
  219. add_hotkey('space', print, args=['space was pressed'])
  220. add_hotkey('Space', print, args=['space was pressed'])
  221. add_hotkey('ctrl+q', quit)
  222. add_hotkey('ctrl+alt+enter, space', some_callback)
  223. """
  224. steps = canonicalize(hotkey)
  225. state = _State()
  226. state.step = 0
  227. state.time = _time.time()
  228. def handler(event):
  229. if event.event_type == KEY_UP:
  230. if trigger_on_release and state.step == len(steps):
  231. state.step = 0
  232. callback(*args)
  233. return suppress
  234. return
  235. # Just waiting for the user to release a key.
  236. if trigger_on_release and state.step >= len(steps):
  237. return
  238. timed_out = state.step > 0 and timeout and event.time - state.time > timeout
  239. unexpected = not any(matches(event, part) for part in steps[state.step])
  240. min_time_in = state.step > 0 and min_time and event.time - state.time < min_time
  241. if unexpected or timed_out or min_time_in:
  242. if state.step > 0:
  243. state.step = 0
  244. # Could be start of hotkey again.
  245. handler(event)
  246. else:
  247. state.step = 0
  248. else:
  249. state.time = event.time
  250. if all(is_pressed(part) or matches(event, part) for part in steps[state.step]):
  251. state.step += 1
  252. if not trigger_on_release and state.step == len(steps):
  253. state.step = 0
  254. callback(*args)
  255. return suppress
  256. _hotkeys[hotkey] = handler
  257. if suppress:
  258. _suppress_hotkey(steps, timeout)
  259. _hotkeys_suppressed[hotkey] = timeout
  260. return hook(handler)
  261. # Alias.
  262. register_hotkey = add_hotkey
  263. def hook(callback):
  264. """
  265. Installs a global listener on all available keyboards, invoking `callback`
  266. each time a key is pressed or released.
  267. The event passed to the callback is of type `keyboard.KeyboardEvent`,
  268. with the following attributes:
  269. - `name`: an Unicode representation of the character (e.g. "&") or
  270. description (e.g. "space"). The name is always lower-case.
  271. - `scan_code`: number representing the physical key, e.g. 55.
  272. - `time`: timestamp of the time the event occurred, with as much precision
  273. as given by the OS.
  274. Returns the given callback for easier development.
  275. """
  276. _listener.add_handler(callback)
  277. return callback
  278. def unhook(callback):
  279. """ Removes a previously hooked callback. """
  280. _listener.remove_handler(callback)
  281. def unhook_all():
  282. """
  283. Removes all keyboard hooks in use, including hotkeys, abbreviations, word
  284. listeners, `record`ers and `wait`s.
  285. """
  286. _hotkeys.clear()
  287. _word_listeners.clear()
  288. del _listener.handlers[:]
  289. def hook_key(key, keydown_callback=lambda: None, keyup_callback=lambda: None):
  290. """
  291. Hooks key up and key down events for a single key. Returns the event handler
  292. created. To remove a hooked key use `unhook_key(key)` or
  293. `unhook_key(handler)`.
  294. Note: this function shares state with hotkeys, so `clear_all_hotkeys`
  295. affects it aswell.
  296. """
  297. def handler(event):
  298. if not matches(event, key):
  299. return
  300. if event.event_type == KEY_DOWN:
  301. keydown_callback()
  302. if event.event_type == KEY_UP:
  303. keyup_callback()
  304. _hotkeys[key] = handler
  305. return hook(handler)
  306. def on_press(callback):
  307. """
  308. Invokes `callback` for every KEY_DOWN event. For details see `hook`.
  309. """
  310. return hook(lambda e: e.event_type == KEY_DOWN and callback(e))
  311. def on_release(callback):
  312. """
  313. Invokes `callback` for every KEY_UP event. For details see `hook`.
  314. """
  315. return hook(lambda e: e.event_type == KEY_UP and callback(e))
  316. def _remove_named_hook(name_or_handler, names):
  317. """
  318. Removes a hook that was registered with a given name in a dictionary.
  319. """
  320. if callable(name_or_handler):
  321. handler = name_or_handler
  322. try:
  323. name = next(n for n, h in names.items() if h == handler)
  324. except StopIteration:
  325. raise ValueError('This handler is not associated with any name.')
  326. unhook(handler)
  327. del names[name]
  328. else:
  329. name = name_or_handler
  330. try:
  331. handler = names[name]
  332. except KeyError as e:
  333. raise ValueError('No such named listener: ' + repr(name), e)
  334. unhook(names[name])
  335. del names[name]
  336. return name
  337. def remove_hotkey(hotkey_or_handler):
  338. """
  339. Removes a previously registered hotkey. Accepts either the hotkey used
  340. during registration (exact string) or the event handler returned by the
  341. `add_hotkey` or `hook_key` functions.
  342. """
  343. name = _remove_named_hook(hotkey_or_handler, _hotkeys)
  344. if name in _hotkeys_suppressed:
  345. del _hotkeys_suppressed[name]
  346. # because the table structure is optimized for runtime, we must recompile
  347. _key_table.suppress_none()
  348. for current_name in _hotkeys_suppressed:
  349. _suppress_hotkey(canonicalize(current_name), _hotkeys_suppressed[current_name])
  350. # Alias.
  351. unhook_key = remove_hotkey
  352. _word_listeners = {}
  353. def add_word_listener(word, callback, triggers=['space'], match_suffix=False, timeout=2):
  354. """
  355. Invokes a callback every time a sequence of characters is typed (e.g. 'pet')
  356. and followed by a trigger key (e.g. space). Modifiers (e.g. alt, ctrl,
  357. shift) are ignored.
  358. - `word` the typed text to be matched. E.g. 'pet'.
  359. - `callback` is an argument-less function to be invoked each time the word
  360. is typed.
  361. - `triggers` is the list of keys that will cause a match to be checked. If
  362. the user presses some key that is not a character (len>1) and not in
  363. triggers, the characters so far will be discarded. By default only space
  364. bar triggers match checks.
  365. - `match_suffix` defines if endings of words should also be checked instead
  366. of only whole words. E.g. if true, typing 'carpet'+space will trigger the
  367. listener for 'pet'. Defaults to false, only whole words are checked.
  368. - `timeout` is the maximum number of seconds between typed characters before
  369. the current word is discarded. Defaults to 2 seconds.
  370. Returns the event handler created. To remove a word listener use
  371. `remove_word_listener(word)` or `remove_word_listener(handler)`.
  372. Note: all actions are performed on key down. Key up events are ignored.
  373. Note: word mathes are **case sensitive**.
  374. """
  375. if word in _word_listeners:
  376. raise ValueError('Already listening for word {}'.format(repr(word)))
  377. state = _State()
  378. state.current = ''
  379. state.time = _time.time()
  380. def handler(event):
  381. name = event.name
  382. if event.event_type == KEY_UP or name in all_modifiers: return
  383. matched = state.current == word or (match_suffix and state.current.endswith(word))
  384. if name in triggers and matched:
  385. callback()
  386. state.current = ''
  387. elif len(name) > 1:
  388. state.current = ''
  389. else:
  390. if timeout and event.time - state.time > timeout:
  391. state.current = ''
  392. state.time = event.time
  393. state.current += name if not is_pressed('shift') else name.upper()
  394. _word_listeners[word] = hook(handler)
  395. return handler
  396. def remove_word_listener(word_or_handler):
  397. """
  398. Removes a previously registered word listener. Accepts either the word used
  399. during registration (exact string) or the event handler returned by the
  400. `add_word_listener` or `add_abbreviation` functions.
  401. """
  402. _remove_named_hook(word_or_handler, _word_listeners)
  403. def add_abbreviation(source_text, replacement_text, match_suffix=False, timeout=2):
  404. """
  405. Registers a hotkey that replaces one typed text with another. For example
  406. add_abbreviation('tm', u'™')
  407. Replaces every "tm" followed by a space with a ™ symbol (and no space). The
  408. replacement is done by sending backspace events.
  409. - `match_suffix` defines if endings of words should also be checked instead
  410. of only whole words. E.g. if true, typing 'carpet'+space will trigger the
  411. listener for 'pet'. Defaults to false, only whole words are checked.
  412. - `timeout` is the maximum number of seconds between typed characters before
  413. the current word is discarded. Defaults to 2 seconds.
  414. For more details see `add_word_listener`.
  415. """
  416. replacement = '\b'*(len(source_text)+1) + replacement_text
  417. callback = lambda: write(replacement, restore_state_after=False)
  418. return add_word_listener(source_text, callback, match_suffix=match_suffix, timeout=timeout)
  419. # Aliases.
  420. register_word_listener = add_word_listener
  421. register_abbreviation = add_abbreviation
  422. remove_abbreviation = remove_word_listener
  423. def stash_state():
  424. """
  425. Builds a list of all currently pressed scan codes, releases them and returns
  426. the list. Pairs well with `restore_state`.
  427. """
  428. state = sorted(_pressed_events)
  429. for scan_code in state:
  430. _os_keyboard.release(scan_code)
  431. return state
  432. def restore_state(scan_codes):
  433. """
  434. Given a list of scan_codes ensures these keys, and only these keys, are
  435. pressed. Pairs well with `stash_state`.
  436. """
  437. current = set(_pressed_events)
  438. target = set(scan_codes)
  439. for scan_code in current - target:
  440. _os_keyboard.release(scan_code)
  441. for scan_code in target - current:
  442. _os_keyboard.press(scan_code)
  443. def write(text, delay=0, restore_state_after=True, exact=False):
  444. """
  445. Sends artificial keyboard events to the OS, simulating the typing of a given
  446. text. Characters not available on the keyboard are typed as explicit unicode
  447. characters using OS-specific functionality, such as alt+codepoint.
  448. To ensure text integrity all currently pressed keys are released before
  449. the text is typed.
  450. - `delay` is the number of seconds to wait between keypresses, defaults to
  451. no delay.
  452. - `restore_state_after` can be used to restore the state of pressed keys
  453. after the text is typed, i.e. presses the keys that were released at the
  454. beginning. Defaults to True.
  455. - `exact` forces typing all characters as explicit unicode (e.g. alt+codepoint)
  456. """
  457. state = stash_state()
  458. if exact:
  459. for letter in text:
  460. _os_keyboard.type_unicode(letter)
  461. if delay: _time.sleep(delay)
  462. else:
  463. for letter in text:
  464. try:
  465. if letter in '\n\b\t ':
  466. letter = _normalize_name(letter)
  467. scan_code, modifiers = _os_keyboard.map_char(letter)
  468. if is_pressed(scan_code):
  469. release(scan_code)
  470. for modifier in modifiers:
  471. press(modifier)
  472. _os_keyboard.press(scan_code)
  473. _os_keyboard.release(scan_code)
  474. for modifier in modifiers:
  475. release(modifier)
  476. except ValueError:
  477. _os_keyboard.type_unicode(letter)
  478. if delay:
  479. _time.sleep(delay)
  480. if restore_state_after:
  481. restore_state(state)
  482. def to_scan_code(key):
  483. """
  484. Returns the scan code for a given key name (or scan code, i.e. do nothing).
  485. Note that a name may belong to more than one physical key, in which case
  486. one of the scan codes will be chosen.
  487. """
  488. if is_number(key):
  489. return key
  490. else:
  491. scan_code, modifiers = _os_keyboard.map_char(_normalize_name(key))
  492. return scan_code
  493. def send(combination, do_press=True, do_release=True):
  494. """
  495. Sends OS events that perform the given hotkey combination.
  496. - `combination` can be either a scan code (e.g. 57 for space), single key
  497. (e.g. 'space') or multi-key, multi-step combination (e.g. 'alt+F4, enter').
  498. - `do_press` if true then press events are sent. Defaults to True.
  499. - `do_release` if true then release events are sent. Defaults to True.
  500. send(57)
  501. send('ctrl+alt+del')
  502. send('alt+F4, enter')
  503. send('shift+s')
  504. Note: keys are released in the opposite order they were pressed.
  505. """
  506. for keys in canonicalize(combination):
  507. if do_press:
  508. for key in keys:
  509. _os_keyboard.press(to_scan_code(key))
  510. if do_release:
  511. for key in reversed(keys):
  512. _os_keyboard.release(to_scan_code(key))
  513. def press(combination):
  514. """ Presses and holds down a key combination (see `send`). """
  515. send(combination, True, False)
  516. def release(combination):
  517. """ Releases a key combination (see `send`). """
  518. send(combination, False, True)
  519. def press_and_release(combination):
  520. """ Presses and releases the key combination (see `send`). """
  521. send(combination, True, True)
  522. def _make_wait_and_unlock():
  523. """
  524. Method to work around CPython's inability to interrupt Lock.join with
  525. signals. Without this Ctrl+C doesn't close the program.
  526. """
  527. q = queue.Queue(maxsize=1)
  528. def wait():
  529. while True:
  530. try:
  531. return q.get(timeout=1)
  532. except queue.Empty:
  533. pass
  534. return (wait, lambda v=None: q.put(v))
  535. def wait(combination=None):
  536. """
  537. Blocks the program execution until the given key combination is pressed or,
  538. if given no parameters, blocks forever.
  539. """
  540. wait, unlock = _make_wait_and_unlock()
  541. if combination is not None:
  542. hotkey_handler = add_hotkey(combination, unlock)
  543. wait()
  544. remove_hotkey(hotkey_handler)
  545. def read_key(filter=lambda e: True):
  546. """
  547. Blocks until a keyboard event happens, then returns that event.
  548. """
  549. wait, unlock = _make_wait_and_unlock()
  550. def test(event):
  551. if filter(event):
  552. unhook(test)
  553. unlock(event)
  554. hook(test)
  555. return wait()
  556. def record(until='escape'):
  557. """
  558. Records all keyboard events from all keyboards until the user presses the
  559. given key combination. Then returns the list of events recorded, of type
  560. `keyboard.KeyboardEvent`. Pairs well with
  561. `play(events)`.
  562. Note: this is a blocking function.
  563. Note: for more details on the keyboard hook and events see `hook`.
  564. """
  565. recorded = []
  566. hook(recorded.append)
  567. wait(until)
  568. unhook(recorded.append)
  569. return recorded
  570. def play(events, speed_factor=1.0):
  571. """
  572. Plays a sequence of recorded events, maintaining the relative time
  573. intervals. If speed_factor is <= 0 then the actions are replayed as fast
  574. as the OS allows. Pairs well with `record()`.
  575. Note: the current keyboard state is cleared at the beginning and restored at
  576. the end of the function.
  577. """
  578. state = stash_state()
  579. last_time = None
  580. for event in events:
  581. if speed_factor > 0 and last_time is not None:
  582. _time.sleep((event.time - last_time) / speed_factor)
  583. last_time = event.time
  584. key = event.scan_code or event.name
  585. if event.event_type == KEY_DOWN:
  586. press(key)
  587. elif event.event_type == KEY_UP:
  588. release(key)
  589. # Ignore other types of events.
  590. restore_state(state)
  591. replay = play
  592. def get_typed_strings(events, allow_backspace=True):
  593. """
  594. Given a sequence of events, tries to deduce what strings were typed.
  595. Strings are separated when a non-textual key is pressed (such as tab or
  596. enter). Characters are converted to uppercase according to shift and
  597. capslock status. If `allow_backspace` is True, backspaces remove the last
  598. character typed.
  599. This function is a generator, so you can pass an infinite stream of events
  600. and convert them to strings in real time.
  601. Note this functions is merely an heuristic. Windows for example keeps per-
  602. process keyboard state such as keyboard layout, and this information is not
  603. available for our hooks.
  604. get_type_strings(record()) -> ['This is what', 'I recorded', '']
  605. """
  606. shift_pressed = False
  607. capslock_pressed = False
  608. string = ''
  609. for event in events:
  610. name = event.name
  611. # Space is the only key that we canonicalize to the spelled out name
  612. # because of legibility. Now we have to undo that.
  613. if matches(event, 'space'):
  614. name = ' '
  615. if matches(event, 'shift'):
  616. shift_pressed = event.event_type == 'down'
  617. elif matches(event, 'caps lock') and event.event_type == 'down':
  618. capslock_pressed = not capslock_pressed
  619. elif allow_backspace and matches(event, 'backspace') and event.event_type == 'down':
  620. string = string[:-1]
  621. elif event.event_type == 'down':
  622. if len(name) == 1:
  623. if shift_pressed ^ capslock_pressed:
  624. name = name.upper()
  625. string = string + name
  626. else:
  627. yield string
  628. string = ''
  629. yield string
  630. _key_table = _KeyTable(press, release)
  631. recording = None
  632. def start_recording(recorded_events_queue=None):
  633. """
  634. Starts recording all keyboard events into a global variable, or the given
  635. queue if any. Returns the queue of events and the hooked function.
  636. Use `stop_recording()` or `unhook(hooked_function)` to stop.
  637. """
  638. global recording
  639. recorded_events_queue = recorded_events_queue or queue.Queue()
  640. recording = recorded_events_queue, hook(recorded_events_queue.put)
  641. return recording
  642. def stop_recording():
  643. """
  644. Stops the global recording of events and returns a list of the events
  645. captured.
  646. """
  647. global recording
  648. if not recording:
  649. raise ValueError('Must call "start_recording" before.')
  650. recorded_events_queue, hooked = recording
  651. unhook(hooked)
  652. recording = None
  653. return list(recorded_events_queue.queue)
  654. def get_shortcut_name(names=None):
  655. """
  656. Returns a string representation of shortcut from the given key names, or
  657. the currently pressed keys if not given. This function:
  658. - normalizes names;
  659. - removes "left" and "right" prefixes;
  660. - replaces the "+" key name with "plus" to avoid ambiguity;
  661. - puts modifier keys first, in a standardized order;
  662. - sort remaining keys;
  663. - finally, joins everything with "+".
  664. Example:
  665. get_shortcut_name(['+', 'left ctrl', 'shift'])
  666. # "ctrl+shift+plus"
  667. """
  668. if names is None:
  669. _listener.start_if_necessary()
  670. names = [e.name for e in _pressed_events.values()]
  671. else:
  672. names = [_normalize_name(name) for name in names]
  673. clean_names = set(e.replace('left ', '').replace('right ', '').replace('+', 'plus') for e in names)
  674. # https://developer.apple.com/macos/human-interface-guidelines/input-and-output/keyboard/
  675. # > List modifier keys in the correct order. If you use more than one modifier key in a
  676. # > shortcut, always list them in this order: Control, Option, Shift, Command.
  677. modifiers = ['ctrl', 'alt', 'shift', 'windows']
  678. sorting_key = lambda k: (modifiers.index(k) if k in modifiers else 5, str(k))
  679. return '+'.join(sorted(clean_names, key=sorting_key))
  680. def read_shortcut():
  681. """
  682. Similar to `read_key()`, but blocks until the user presses and releases a key
  683. combination (or single key), then returns a string representing the shortcut
  684. pressed.
  685. Example:
  686. read_shortcut()
  687. # "ctrl+shift+p"
  688. """
  689. wait, unlock = _make_wait_and_unlock()
  690. def test(event):
  691. if event.event_type == KEY_UP:
  692. unhook(test)
  693. names = [e.name for e in _pressed_events.values()] + [event.name]
  694. unlock(get_shortcut_name(names))
  695. hook(test)
  696. return wait()
  697. read_hotkey = read_shortcut