{"id":13448201,"url":"https://github.com/boppreh/keyboard","last_synced_at":"2025-05-13T18:13:14.297Z","repository":{"id":8359454,"uuid":"9923468","full_name":"boppreh/keyboard","owner":"boppreh","description":"Hook and simulate global keyboard events on Windows and Linux.","archived":false,"fork":false,"pushed_at":"2024-06-19T15:14:03.000Z","size":1407,"stargazers_count":3880,"open_issues_count":414,"forks_count":441,"subscribers_count":75,"default_branch":"master","last_synced_at":"2025-05-12T04:45:05.224Z","etag":null,"topics":["callback","hotkey","keyboard-events","keyboard-hooks","keyboard-state","python","register-hotkey","scancode"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/boppreh.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGES.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2013-05-07T22:55:07.000Z","updated_at":"2025-05-09T20:39:16.000Z","dependencies_parsed_at":"2024-04-28T01:53:31.103Z","dependency_job_id":"b3c3110c-f0c6-4387-8a8d-6a36c7f2cae1","html_url":"https://github.com/boppreh/keyboard","commit_stats":{"total_commits":722,"total_committers":42,"mean_commits":17.19047619047619,"dds":"0.13434903047091418","last_synced_commit":"d232de09bda50ecb5211ebcc59b85bc6da6aaa24"},"previous_names":[],"tags_count":26,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/boppreh%2Fkeyboard","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/boppreh%2Fkeyboard/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/boppreh%2Fkeyboard/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/boppreh%2Fkeyboard/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/boppreh","download_url":"https://codeload.github.com/boppreh/keyboard/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254000883,"owners_count":21997443,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["callback","hotkey","keyboard-events","keyboard-hooks","keyboard-state","python","register-hotkey","scancode"],"created_at":"2024-07-31T05:01:38.656Z","updated_at":"2025-05-13T18:13:14.238Z","avatar_url":"https://github.com/boppreh.png","language":"Python","readme":"**This project is currently unmaintained. It works for many cases, and I wish to pick it up again in the future, but you might encounter some friction and limited features using it.**\n\n---\n\n---\n\nkeyboard\n========\n\nTake full control of your keyboard with this small Python library. Hook global events, register hotkeys, simulate key presses and much more.\n\n## Features\n\n- **Global event hook** on all keyboards (captures keys regardless of focus).\n- **Listen** and **send** keyboard events.\n- Works with **Windows** and **Linux** (requires sudo), with experimental **OS X** support (thanks @glitchassassin!).\n- **Pure Python**, no C modules to be compiled.\n- **Zero dependencies**. Trivial to install and deploy, just copy the files.\n- **Python 2 and 3**.\n- Complex hotkey support (e.g. `ctrl+shift+m, ctrl+space`) with controllable timeout.\n- Includes **high level API** (e.g. [record](#keyboard.record) and [play](#keyboard.play), [add_abbreviation](#keyboard.add_abbreviation)).\n- Maps keys as they actually are in your layout, with **full internationalization support** (e.g. `Ctrl+ç`).\n- Events automatically captured in separate thread, doesn't block main program.\n- Tested and documented.\n- Doesn't break accented dead keys (I'm looking at you, pyHook).\n- Mouse support available via project [mouse](https://github.com/boppreh/mouse) (`pip install mouse`).\n\n## Usage\n\nInstall the [PyPI package](https://pypi.python.org/pypi/keyboard/):\n\n    pip install keyboard\n\nor clone the repository (no installation required, source files are sufficient):\n\n    git clone https://github.com/boppreh/keyboard\n\nor [download and extract the zip](https://github.com/boppreh/keyboard/archive/master.zip) into your project folder.\n\nThen check the [API docs below](https://github.com/boppreh/keyboard#api) to see what features are available.\n\n\n## Example\n\nUse as library:\n\n```py\nimport keyboard\n\nkeyboard.press_and_release('shift+s, space')\n\nkeyboard.write('The quick brown fox jumps over the lazy dog.')\n\nkeyboard.add_hotkey('ctrl+shift+a', print, args=('triggered', 'hotkey'))\n\n# Press PAGE UP then PAGE DOWN to type \"foobar\".\nkeyboard.add_hotkey('page up, page down', lambda: keyboard.write('foobar'))\n\n# Blocks until you press esc.\nkeyboard.wait('esc')\n\n# Record events until 'esc' is pressed.\nrecorded = keyboard.record(until='esc')\n# Then replay back at three times the speed.\nkeyboard.play(recorded, speed_factor=3)\n\n# Type @@ then press space to replace with abbreviation.\nkeyboard.add_abbreviation('@@', 'my.long.email@example.com')\n\n# Block forever, like `while True`.\nkeyboard.wait()\n```\n\nUse as standalone module:\n\n```bash\n# Save JSON events to a file until interrupted:\npython -m keyboard \u003e events.txt\n\ncat events.txt\n# {\"event_type\": \"down\", \"scan_code\": 25, \"name\": \"p\", \"time\": 1622447562.2994788, \"is_keypad\": false}\n# {\"event_type\": \"up\", \"scan_code\": 25, \"name\": \"p\", \"time\": 1622447562.431007, \"is_keypad\": false}\n# ...\n\n# Replay events\npython -m keyboard \u003c events.txt\n```\n\n## Known limitations:\n\n- Events generated under Windows don't report device id (`event.device == None`). [#21](https://github.com/boppreh/keyboard/issues/21)\n- Media keys on Linux may appear nameless (scan-code only) or not at all. [#20](https://github.com/boppreh/keyboard/issues/20)\n- Key suppression/blocking only available on Windows. [#22](https://github.com/boppreh/keyboard/issues/22)\n- To avoid depending on X, the Linux parts reads raw device files (`/dev/input/input*`) but this requires root.\n- Other applications, such as some games, may register hooks that swallow all key events. In this case `keyboard` will be unable to report events.\n- This program makes no attempt to hide itself, so don't use it for keyloggers or online gaming bots. Be responsible.\n- SSH connections forward only the text typed, not keyboard events. Therefore if you connect to a server or Raspberry PI that is running `keyboard` via SSH, the server will not detect your key events.\n\n## Common patterns and mistakes\n\n### Preventing the program from closing\n\n```py\nimport keyboard\nkeyboard.add_hotkey('space', lambda: print('space was pressed!'))\n# If the program finishes, the hotkey is not in effect anymore.\n\n# Don't do this! This will use 100% of your CPU.\n#while True: pass\n\n# Use this instead\nkeyboard.wait()\n\n# or this\nimport time\nwhile True:\n    time.sleep(1000000)\n```\n\n### Waiting for a key press one time\n\n```py\nimport keyboard\n\n# Don't do this! This will use 100% of your CPU until you press the key.\n#\n#while not keyboard.is_pressed('space'):\n#    continue\n#print('space was pressed, continuing...')\n\n# Do this instead\nkeyboard.wait('space')\nprint('space was pressed, continuing...')\n```\n\n### Repeatedly waiting for a key press\n\n```py\nimport keyboard\n\n# Don't do this!\n#\n#while True:\n#    if keyboard.is_pressed('space'):\n#        print('space was pressed!')\n#\n# This will use 100% of your CPU and print the message many times.\n\n# Do this instead\nwhile True:\n    keyboard.wait('space')\n    print('space was pressed! Waiting on it again...')\n\n# or this\nkeyboard.add_hotkey('space', lambda: print('space was pressed!'))\nkeyboard.wait()\n```\n\n### Invoking code when an event happens\n\n```py\nimport keyboard\n\n# Don't do this! This will call `print('space')` immediately then fail when the key is actually pressed.\n#keyboard.add_hotkey('space', print('space was pressed'))\n\n# Do this instead\nkeyboard.add_hotkey('space', lambda: print('space was pressed'))\n\n# or this\ndef on_space():\n    print('space was pressed')\nkeyboard.add_hotkey('space', on_space)\n\n# or this\nwhile True:\n    # Wait for the next event.\n    event = keyboard.read_event()\n    if event.event_type == keyboard.KEY_DOWN and event.name == 'space':\n        print('space was pressed')\n```\n\n### 'Press any key to continue'\n\n```py\n# Don't do this! The `keyboard` module is meant for global events, even when your program is not in focus.\n#import keyboard\n#print('Press any key to continue...')\n#keyboard.get_event()\n\n# Do this instead\ninput('Press enter to continue...')\n\n# Or one of the suggestions from here\n# https://stackoverflow.com/questions/983354/how-to-make-a-script-wait-for-a-pressed-key\n```\n\n\n\n# API\n#### Table of Contents\n\n- [keyboard.**KEY\\_DOWN**](#keyboard.KEY_DOWN)\n- [keyboard.**KEY\\_UP**](#keyboard.KEY_UP)\n- [keyboard.**KeyboardEvent**](#keyboard.KeyboardEvent)\n- [keyboard.**all\\_modifiers**](#keyboard.all_modifiers)\n- [keyboard.**sided\\_modifiers**](#keyboard.sided_modifiers)\n- [keyboard.**version**](#keyboard.version)\n- [keyboard.**is\\_modifier**](#keyboard.is_modifier)\n- [keyboard.**key\\_to\\_scan\\_codes**](#keyboard.key_to_scan_codes)\n- [keyboard.**parse\\_hotkey**](#keyboard.parse_hotkey)\n- [keyboard.**send**](#keyboard.send) *(aliases: `press_and_release`)*\n- [keyboard.**press**](#keyboard.press)\n- [keyboard.**release**](#keyboard.release)\n- [keyboard.**is\\_pressed**](#keyboard.is_pressed)\n- [keyboard.**call\\_later**](#keyboard.call_later)\n- [keyboard.**hook**](#keyboard.hook)\n- [keyboard.**on\\_press**](#keyboard.on_press)\n- [keyboard.**on\\_release**](#keyboard.on_release)\n- [keyboard.**hook\\_key**](#keyboard.hook_key)\n- [keyboard.**on\\_press\\_key**](#keyboard.on_press_key)\n- [keyboard.**on\\_release\\_key**](#keyboard.on_release_key)\n- [keyboard.**unhook**](#keyboard.unhook) *(aliases: `unblock_key`, `unhook_key`, `unremap_key`)*\n- [keyboard.**unhook\\_all**](#keyboard.unhook_all)\n- [keyboard.**block\\_key**](#keyboard.block_key)\n- [keyboard.**remap\\_key**](#keyboard.remap_key)\n- [keyboard.**parse\\_hotkey\\_combinations**](#keyboard.parse_hotkey_combinations)\n- [keyboard.**add\\_hotkey**](#keyboard.add_hotkey) *(aliases: `register_hotkey`)*\n- [keyboard.**remove\\_hotkey**](#keyboard.remove_hotkey) *(aliases: `clear_hotkey`, `unregister_hotkey`, `unremap_hotkey`)*\n- [keyboard.**unhook\\_all\\_hotkeys**](#keyboard.unhook_all_hotkeys) *(aliases: `clear_all_hotkeys`, `remove_all_hotkeys`, `unregister_all_hotkeys`)*\n- [keyboard.**remap\\_hotkey**](#keyboard.remap_hotkey)\n- [keyboard.**stash\\_state**](#keyboard.stash_state)\n- [keyboard.**restore\\_state**](#keyboard.restore_state)\n- [keyboard.**restore\\_modifiers**](#keyboard.restore_modifiers)\n- [keyboard.**write**](#keyboard.write)\n- [keyboard.**wait**](#keyboard.wait)\n- [keyboard.**get\\_hotkey\\_name**](#keyboard.get_hotkey_name)\n- [keyboard.**read\\_event**](#keyboard.read_event)\n- [keyboard.**read\\_key**](#keyboard.read_key)\n- [keyboard.**read\\_hotkey**](#keyboard.read_hotkey)\n- [keyboard.**get\\_typed\\_strings**](#keyboard.get_typed_strings)\n- [keyboard.**start\\_recording**](#keyboard.start_recording)\n- [keyboard.**stop\\_recording**](#keyboard.stop_recording)\n- [keyboard.**record**](#keyboard.record)\n- [keyboard.**play**](#keyboard.play) *(aliases: `replay`)*\n- [keyboard.**add\\_word\\_listener**](#keyboard.add_word_listener) *(aliases: `register_word_listener`)*\n- [keyboard.**remove\\_word\\_listener**](#keyboard.remove_word_listener) *(aliases: `remove_abbreviation`)*\n- [keyboard.**add\\_abbreviation**](#keyboard.add_abbreviation) *(aliases: `register_abbreviation`)*\n- [keyboard.**normalize\\_name**](#keyboard.normalize_name)\n\n\n\u003ca name=\"keyboard.KEY_DOWN\"/\u003e\n\n## keyboard.**KEY\\_DOWN**\n```py\n= 'down'\n```\n\n\u003ca name=\"keyboard.KEY_UP\"/\u003e\n\n## keyboard.**KEY\\_UP**\n```py\n= 'up'\n```\n\n\u003ca name=\"keyboard.KeyboardEvent\"/\u003e\n\n## class keyboard.**KeyboardEvent**\n\n\n\n\n\u003ca name=\"KeyboardEvent.device\"/\u003e\n\n### KeyboardEvent.**device**\n\n\n\u003ca name=\"KeyboardEvent.event_type\"/\u003e\n\n### KeyboardEvent.**event\\_type**\n\n\n\u003ca name=\"KeyboardEvent.is_keypad\"/\u003e\n\n### KeyboardEvent.**is\\_keypad**\n\n\n\u003ca name=\"KeyboardEvent.modifiers\"/\u003e\n\n### KeyboardEvent.**modifiers**\n\n\n\u003ca name=\"KeyboardEvent.name\"/\u003e\n\n### KeyboardEvent.**name**\n\n\n\u003ca name=\"KeyboardEvent.scan_code\"/\u003e\n\n### KeyboardEvent.**scan\\_code**\n\n\n\u003ca name=\"KeyboardEvent.time\"/\u003e\n\n### KeyboardEvent.**time**\n\n\n\u003ca name=\"KeyboardEvent.to_json\"/\u003e\n\n### KeyboardEvent.**to\\_json**(self, ensure\\_ascii=False)\n\n[\\[source\\]](https://github.com/boppreh/keyboard/blob/master/keyboard/_keyboard_event.py#L34)\n\n\n\n\n\n\n\u003ca name=\"keyboard.all_modifiers\"/\u003e\n\n## keyboard.**all\\_modifiers**\n```py\n= {'alt', 'alt gr', 'ctrl', 'left alt', 'left ctrl', 'left shift', 'left windows', 'right alt', 'right ctrl', 'right shift', 'right windows', 'shift', 'windows'}\n```\n\n\u003ca name=\"keyboard.sided_modifiers\"/\u003e\n\n## keyboard.**sided\\_modifiers**\n```py\n= {'alt', 'ctrl', 'shift', 'windows'}\n```\n\n\u003ca name=\"keyboard.version\"/\u003e\n\n## keyboard.**version**\n```py\n= '0.13.5'\n```\n\n\u003ca name=\"keyboard.is_modifier\"/\u003e\n\n## keyboard.**is\\_modifier**(key)\n\n[\\[source\\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L242)\n\n\nReturns True if `key` is a scan code or name of a modifier key.\n\n\n\n\u003ca name=\"keyboard.key_to_scan_codes\"/\u003e\n\n## keyboard.**key\\_to\\_scan\\_codes**(key, error\\_if\\_missing=True)\n\n[\\[source\\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L405)\n\n\nReturns a list of scan codes associated with this key (name or scan code).\n\n\n\n\u003ca name=\"keyboard.parse_hotkey\"/\u003e\n\n## keyboard.**parse\\_hotkey**(hotkey)\n\n[\\[source\\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L435)\n\n\nParses a user-provided hotkey into nested tuples representing the\nparsed structure, with the bottom values being lists of scan codes.\nAlso accepts raw scan codes, which are then wrapped in the required\nnumber of nestings.\n\nExample:\n\n```py\n\nparse_hotkey(\"alt+shift+a, alt+b, c\")\n#    Keys:    ^~^ ^~~~^ ^  ^~^ ^  ^\n#    Steps:   ^~~~~~~~~~^  ^~~~^  ^\n\n# ((alt_codes, shift_codes, a_codes), (alt_codes, b_codes), (c_codes,))\n```\n\n\n\n\u003ca name=\"keyboard.send\"/\u003e\n\n## keyboard.**send**(hotkey, do\\_press=True, do\\_release=True)\n\n[\\[source\\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L468)\n\n\nSends OS events that perform the given *hotkey* hotkey.\n\n- `hotkey` can be either a scan code (e.g. 57 for space), single key\n(e.g. 'space') or multi-key, multi-step hotkey (e.g. 'alt+F4, enter').\n- `do_press` if true then press events are sent. Defaults to True.\n- `do_release` if true then release events are sent. Defaults to True.\n\n```py\n\nsend(57)\nsend('ctrl+alt+del')\nsend('alt+F4, enter')\nsend('shift+s')\n```\n\nNote: keys are released in the opposite order they were pressed.\n\n\n\n\u003ca name=\"keyboard.press\"/\u003e\n\n## keyboard.**press**(hotkey)\n\n[\\[source\\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L501)\n\nPresses and holds down a hotkey (see [`send`](#keyboard.send)). \n\n\n\u003ca name=\"keyboard.release\"/\u003e\n\n## keyboard.**release**(hotkey)\n\n[\\[source\\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L505)\n\nReleases a hotkey (see [`send`](#keyboard.send)). \n\n\n\u003ca name=\"keyboard.is_pressed\"/\u003e\n\n## keyboard.**is\\_pressed**(hotkey)\n\n[\\[source\\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L509)\n\n\nReturns True if the key is pressed.\n\n```py\n\nis_pressed(57) #-\u003e True\nis_pressed('space') #-\u003e True\nis_pressed('ctrl+space') #-\u003e True\n```\n\n\n\n\u003ca name=\"keyboard.call_later\"/\u003e\n\n## keyboard.**call\\_later**(fn, args=(), delay=0.001)\n\n[\\[source\\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L536)\n\n\nCalls the provided function in a new thread after waiting some time.\nUseful for giving the system some time to process an event, without blocking\nthe current execution flow.\n\n\n\n\u003ca name=\"keyboard.hook\"/\u003e\n\n## keyboard.**hook**(callback, suppress=False, on\\_remove=\u0026lt;lambda\u0026gt;)\n\n[\\[source\\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L546)\n\n\nInstalls a global listener on all available keyboards, invoking `callback`\neach time a key is pressed or released.\n\nThe event passed to the callback is of type `keyboard.KeyboardEvent`,\nwith the following attributes:\n\n- `name`: an Unicode representation of the character (e.g. \"\u0026\") or\ndescription (e.g.  \"space\"). The name is always lower-case.\n- `scan_code`: number representing the physical key, e.g. 55.\n- `time`: timestamp of the time the event occurred, with as much precision\nas given by the OS.\n\nReturns the given callback for easier development.\n\n\n\n\u003ca name=\"keyboard.on_press\"/\u003e\n\n## keyboard.**on\\_press**(callback, suppress=False)\n\n[\\[source\\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L577)\n\n\nInvokes `callback` for every KEY_DOWN event. For details see [`hook`](#keyboard.hook).\n\n\n\n\u003ca name=\"keyboard.on_release\"/\u003e\n\n## keyboard.**on\\_release**(callback, suppress=False)\n\n[\\[source\\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L583)\n\n\nInvokes `callback` for every KEY_UP event. For details see [`hook`](#keyboard.hook).\n\n\n\n\u003ca name=\"keyboard.hook_key\"/\u003e\n\n## keyboard.**hook\\_key**(key, callback, suppress=False)\n\n[\\[source\\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L589)\n\n\nHooks key up and key down events for a single key. Returns the event handler\ncreated. To remove a hooked key use [`unhook_key(key)`](#keyboard.unhook_key) or\n[`unhook_key(handler)`](#keyboard.unhook_key).\n\nNote: this function shares state with hotkeys, so [`clear_all_hotkeys`](#keyboard.clear_all_hotkeys)\naffects it as well.\n\n\n\n\u003ca name=\"keyboard.on_press_key\"/\u003e\n\n## keyboard.**on\\_press\\_key**(key, callback, suppress=False)\n\n[\\[source\\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L613)\n\n\nInvokes `callback` for KEY_DOWN event related to the given key. For details see [`hook`](#keyboard.hook).\n\n\n\n\u003ca name=\"keyboard.on_release_key\"/\u003e\n\n## keyboard.**on\\_release\\_key**(key, callback, suppress=False)\n\n[\\[source\\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L619)\n\n\nInvokes `callback` for KEY_UP event related to the given key. For details see [`hook`](#keyboard.hook).\n\n\n\n\u003ca name=\"keyboard.unhook\"/\u003e\n\n## keyboard.**unhook**(remove)\n\n[\\[source\\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L625)\n\n\nRemoves a previously added hook, either by callback or by the return value\nof [`hook`](#keyboard.hook).\n\n\n\n\u003ca name=\"keyboard.unhook_all\"/\u003e\n\n## keyboard.**unhook\\_all**()\n\n[\\[source\\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L633)\n\n\nRemoves all keyboard hooks in use, including hotkeys, abbreviations, word\nlisteners, [`record`](#keyboard.record)ers and [`wait`](#keyboard.wait)s.\n\n\n\n\u003ca name=\"keyboard.block_key\"/\u003e\n\n## keyboard.**block\\_key**(key)\n\n[\\[source\\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L645)\n\n\nSuppresses all key events of the given key, regardless of modifiers.\n\n\n\n\u003ca name=\"keyboard.remap_key\"/\u003e\n\n## keyboard.**remap\\_key**(src, dst)\n\n[\\[source\\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L652)\n\n\nWhenever the key `src` is pressed or released, regardless of modifiers,\npress or release the hotkey `dst` instead.\n\n\n\n\u003ca name=\"keyboard.parse_hotkey_combinations\"/\u003e\n\n## keyboard.**parse\\_hotkey\\_combinations**(hotkey)\n\n[\\[source\\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L666)\n\n\nParses a user-provided hotkey. Differently from [`parse_hotkey`](#keyboard.parse_hotkey),\ninstead of each step being a list of the different scan codes for each key,\neach step is a list of all possible combinations of those scan codes.\n\n\n\n\u003ca name=\"keyboard.add_hotkey\"/\u003e\n\n## keyboard.**add\\_hotkey**(hotkey, callback, args=(), suppress=False, timeout=1, trigger\\_on\\_release=False)\n\n[\\[source\\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L706)\n\n\nInvokes a callback every time a hotkey is pressed. The hotkey must\nbe in the format `ctrl+shift+a, s`. This would trigger when the user holds\nctrl, shift and \"a\" at once, releases, and then presses \"s\". To represent\nliteral commas, pluses, and spaces, use their names ('comma', 'plus',\n'space').\n\n- `args` is an optional list of arguments to passed to the callback during\neach invocation.\n- `suppress` defines if successful triggers should block the keys from being\nsent to other programs.\n- `timeout` is the amount of seconds allowed to pass between key presses.\n- `trigger_on_release` if true, the callback is invoked on key release instead\nof key press.\n\nThe event handler function is returned. To remove a hotkey call\n[`remove_hotkey(hotkey)`](#keyboard.remove_hotkey) or [`remove_hotkey(handler)`](#keyboard.remove_hotkey).\nbefore the hotkey state is reset.\n\nNote: hotkeys are activated when the last key is *pressed*, not released.\nNote: the callback is executed in a separate thread, asynchronously. For an\nexample of how to use a callback synchronously, see [`wait`](#keyboard.wait).\n\nExamples:\n\n```py\n\n# Different but equivalent ways to listen for a spacebar key press.\nadd_hotkey(' ', print, args=['space was pressed'])\nadd_hotkey('space', print, args=['space was pressed'])\nadd_hotkey('Space', print, args=['space was pressed'])\n# Here 57 represents the keyboard code for spacebar; so you will be\n# pressing 'spacebar', not '57' to activate the print function.\nadd_hotkey(57, print, args=['space was pressed'])\n\nadd_hotkey('ctrl+q', quit)\nadd_hotkey('ctrl+alt+enter, space', some_callback)\n```\n\n\n\n\u003ca name=\"keyboard.remove_hotkey\"/\u003e\n\n## keyboard.**remove\\_hotkey**(hotkey\\_or\\_callback)\n\n[\\[source\\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L852)\n\n\nRemoves a previously hooked hotkey. Must be called with the value returned\nby [`add_hotkey`](#keyboard.add_hotkey).\n\n\n\n\u003ca name=\"keyboard.unhook_all_hotkeys\"/\u003e\n\n## keyboard.**unhook\\_all\\_hotkeys**()\n\n[\\[source\\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L860)\n\n\nRemoves all keyboard hotkeys in use, including abbreviations, word listeners,\n[`record`](#keyboard.record)ers and [`wait`](#keyboard.wait)s.\n\n\n\n\u003ca name=\"keyboard.remap_hotkey\"/\u003e\n\n## keyboard.**remap\\_hotkey**(src, dst, suppress=True, trigger\\_on\\_release=False)\n\n[\\[source\\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L871)\n\n\nWhenever the hotkey `src` is pressed, suppress it and send\n`dst` instead.\n\nExample:\n\n```py\n\nremap('alt+w', 'ctrl+up')\n```\n\n\n\n\u003ca name=\"keyboard.stash_state\"/\u003e\n\n## keyboard.**stash\\_state**()\n\n[\\[source\\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L891)\n\n\nBuilds a list of all currently pressed scan codes, releases them and returns\nthe list. Pairs well with [`restore_state`](#keyboard.restore_state) and [`restore_modifiers`](#keyboard.restore_modifiers).\n\n\n\n\u003ca name=\"keyboard.restore_state\"/\u003e\n\n## keyboard.**restore\\_state**(scan\\_codes)\n\n[\\[source\\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L903)\n\n\nGiven a list of scan_codes ensures these keys, and only these keys, are\npressed. Pairs well with [`stash_state`](#keyboard.stash_state), alternative to [`restore_modifiers`](#keyboard.restore_modifiers).\n\n\n\n\u003ca name=\"keyboard.restore_modifiers\"/\u003e\n\n## keyboard.**restore\\_modifiers**(scan\\_codes)\n\n[\\[source\\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L920)\n\n\nLike [`restore_state`](#keyboard.restore_state), but only restores modifier keys.\n\n\n\n\u003ca name=\"keyboard.write\"/\u003e\n\n## keyboard.**write**(text, delay=0, restore\\_state\\_after=True, exact=None)\n\n[\\[source\\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L926)\n\n\nSends artificial keyboard events to the OS, simulating the typing of a given\ntext. Characters not available on the keyboard are typed as explicit unicode\ncharacters using OS-specific functionality, such as alt+codepoint.\n\nTo ensure text integrity, all currently pressed keys are released before\nthe text is typed, and modifiers are restored afterwards.\n\n- `delay` is the number of seconds to wait between keypresses, defaults to\nno delay.\n- `restore_state_after` can be used to restore the state of pressed keys\nafter the text is typed, i.e. presses the keys that were released at the\nbeginning. Defaults to True.\n- `exact` forces typing all characters as explicit unicode (e.g.\nalt+codepoint or special events). If None, uses platform-specific suggested\nvalue.\n\n\n\n\u003ca name=\"keyboard.wait\"/\u003e\n\n## keyboard.**wait**(hotkey=None, suppress=False, trigger\\_on\\_release=False)\n\n[\\[source\\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L981)\n\n\nBlocks the program execution until the given hotkey is pressed or,\nif given no parameters, blocks forever.\n\n\n\n\u003ca name=\"keyboard.get_hotkey_name\"/\u003e\n\n## keyboard.**get\\_hotkey\\_name**(names=None)\n\n[\\[source\\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L995)\n\n\nReturns a string representation of hotkey from the given key names, or\nthe currently pressed keys if not given.  This function:\n\n- normalizes names;\n- removes \"left\" and \"right\" prefixes;\n- replaces the \"+\" key name with \"plus\" to avoid ambiguity;\n- puts modifier keys first, in a standardized order;\n- sort remaining keys;\n- finally, joins everything with \"+\".\n\nExample:\n\n```py\n\nget_hotkey_name(['+', 'left ctrl', 'shift'])\n# \"ctrl+shift+plus\"\n```\n\n\n\n\u003ca name=\"keyboard.read_event\"/\u003e\n\n## keyboard.**read\\_event**(suppress=False)\n\n[\\[source\\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L1026)\n\n\nBlocks until a keyboard event happens, then returns that event.\n\n\n\n\u003ca name=\"keyboard.read_key\"/\u003e\n\n## keyboard.**read\\_key**(suppress=False)\n\n[\\[source\\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L1037)\n\n\nBlocks until a keyboard event happens, then returns that event's name or,\nif missing, its scan code.\n\n\n\n\u003ca name=\"keyboard.read_hotkey\"/\u003e\n\n## keyboard.**read\\_hotkey**(suppress=True)\n\n[\\[source\\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L1045)\n\n\nSimilar to [`read_key()`](#keyboard.read_key), but blocks until the user presses and releases a\nhotkey (or single key), then returns a string representing the hotkey\npressed.\n\nExample:\n\n```py\n\nread_hotkey()\n# \"ctrl+shift+p\"\n```\n\n\n\n\u003ca name=\"keyboard.get_typed_strings\"/\u003e\n\n## keyboard.**get\\_typed\\_strings**(events, allow\\_backspace=True)\n\n[\\[source\\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L1067)\n\n\nGiven a sequence of events, tries to deduce what strings were typed.\nStrings are separated when a non-textual key is pressed (such as tab or\nenter). Characters are converted to uppercase according to shift and\ncapslock status. If `allow_backspace` is True, backspaces remove the last\ncharacter typed.\n\nThis function is a generator, so you can pass an infinite stream of events\nand convert them to strings in real time.\n\nNote this functions is merely an heuristic. Windows for example keeps per-\nprocess keyboard state such as keyboard layout, and this information is not\navailable for our hooks.\n\n```py\n\nget_type_strings(record()) #-\u003e ['This is what', 'I recorded', '']\n```\n\n\n\n\u003ca name=\"keyboard.start_recording\"/\u003e\n\n## keyboard.**start\\_recording**(recorded\\_events\\_queue=None)\n\n[\\[source\\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L1114)\n\n\nStarts recording all keyboard events into a global variable, or the given\nqueue if any. Returns the queue of events and the hooked function.\n\nUse [`stop_recording()`](#keyboard.stop_recording) or [`unhook(hooked_function)`](#keyboard.unhook) to stop.\n\n\n\n\u003ca name=\"keyboard.stop_recording\"/\u003e\n\n## keyboard.**stop\\_recording**()\n\n[\\[source\\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L1126)\n\n\nStops the global recording of events and returns a list of the events\ncaptured.\n\n\n\n\u003ca name=\"keyboard.record\"/\u003e\n\n## keyboard.**record**(until=\u0026#x27;escape\u0026#x27;, suppress=False, trigger\\_on\\_release=False)\n\n[\\[source\\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L1138)\n\n\nRecords all keyboard events from all keyboards until the user presses the\ngiven hotkey. Then returns the list of events recorded, of type\n`keyboard.KeyboardEvent`. Pairs well with\n[`play(events)`](#keyboard.play).\n\nNote: this is a blocking function.\nNote: for more details on the keyboard hook and events see [`hook`](#keyboard.hook).\n\n\n\n\u003ca name=\"keyboard.play\"/\u003e\n\n## keyboard.**play**(events, speed\\_factor=1.0)\n\n[\\[source\\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L1152)\n\n\nPlays a sequence of recorded events, maintaining the relative time\nintervals. If speed_factor is \u003c= 0 then the actions are replayed as fast\nas the OS allows. Pairs well with [`record()`](#keyboard.record).\n\nNote: the current keyboard state is cleared at the beginning and restored at\nthe end of the function.\n\n\n\n\u003ca name=\"keyboard.add_word_listener\"/\u003e\n\n## keyboard.**add\\_word\\_listener**(word, callback, triggers=[\u0026#x27;space\u0026#x27;], match\\_suffix=False, timeout=2)\n\n[\\[source\\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L1176)\n\n\nInvokes a callback every time a sequence of characters is typed (e.g. 'pet')\nand followed by a trigger key (e.g. space). Modifiers (e.g. alt, ctrl,\nshift) are ignored.\n\n- `word` the typed text to be matched. E.g. 'pet'.\n- `callback` is an argument-less function to be invoked each time the word\nis typed.\n- `triggers` is the list of keys that will cause a match to be checked. If\nthe user presses some key that is not a character (len\u003e1) and not in\ntriggers, the characters so far will be discarded. By default the trigger\nis only `space`.\n- `match_suffix` defines if endings of words should also be checked instead\nof only whole words. E.g. if true, typing 'carpet'+space will trigger the\nlistener for 'pet'. Defaults to false, only whole words are checked.\n- `timeout` is the maximum number of seconds between typed characters before\nthe current word is discarded. Defaults to 2 seconds.\n\nReturns the event handler created. To remove a word listener use\n[`remove_word_listener(word)`](#keyboard.remove_word_listener) or [`remove_word_listener(handler)`](#keyboard.remove_word_listener).\n\nNote: all actions are performed on key down. Key up events are ignored.\nNote: word matches are **case sensitive**.\n\n\n\n\u003ca name=\"keyboard.remove_word_listener\"/\u003e\n\n## keyboard.**remove\\_word\\_listener**(word\\_or\\_handler)\n\n[\\[source\\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L1232)\n\n\nRemoves a previously registered word listener. Accepts either the word used\nduring registration (exact string) or the event handler returned by the\n[`add_word_listener`](#keyboard.add_word_listener) or [`add_abbreviation`](#keyboard.add_abbreviation) functions.\n\n\n\n\u003ca name=\"keyboard.add_abbreviation\"/\u003e\n\n## keyboard.**add\\_abbreviation**(source\\_text, replacement\\_text, match\\_suffix=False, timeout=2)\n\n[\\[source\\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L1240)\n\n\nRegisters a hotkey that replaces one typed text with another. For example\n\n```py\n\nadd_abbreviation('tm', u'™')\n```\n\nReplaces every \"tm\" followed by a space with a ™ symbol (and no space). The\nreplacement is done by sending backspace events.\n\n- `match_suffix` defines if endings of words should also be checked instead\nof only whole words. E.g. if true, typing 'carpet'+space will trigger the\nlistener for 'pet'. Defaults to false, only whole words are checked.\n- `timeout` is the maximum number of seconds between typed characters before\nthe current word is discarded. Defaults to 2 seconds.\n\nFor more details see [`add_word_listener`](#keyboard.add_word_listener).\n\n\n\n\u003ca name=\"keyboard.normalize_name\"/\u003e\n\n## keyboard.**normalize\\_name**(name)\n\n[\\[source\\]](https://github.com/boppreh/keyboard/blob/master/keyboard/_canonical_names.py#L1233)\n\n\nGiven a key name (e.g. \"LEFT CONTROL\"), clean up the string and convert to\nthe canonical representation (e.g. \"left ctrl\") if one is known.\n\n\n\n","funding_links":[],"categories":["Python","Hardware","Desktop App Development","硬件","Uncategorized","Hardware [🔝](#readme)","📚 فهرست","\u003ca id=\"1d9dec1320a5d774dc8e0e7604edfcd3\"\u003e\u003c/a\u003e工具-新添加的","资源列表","Awesome Python"],"sub_categories":["Python Toolkit","Uncategorized","کار با صفحه کلید","\u003ca id=\"8f1b9c5c2737493524809684b934d49a\"\u003e\u003c/a\u003e文章\u0026\u0026视频","硬件","Hardware"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fboppreh%2Fkeyboard","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fboppreh%2Fkeyboard","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fboppreh%2Fkeyboard/lists"}