Package: keyboarded
keyboardTake full control of your keyboard with this small Python library. Hook global events, register hotkeys, simulate key presses and much more.FeaturesGlobal event hookon all keyboards (captures keys regardless of focus).Listenandsendkeyboard events.Works withWindowsandLinux(requires sudo), with experimentalOS Xsupport (thanks @glitchassassin!).Pure Python, no C modules to be compiled.Zero dependencies. Trivial to install and deploy, just copy the files.Python 2 and 3.Complex hotkey support (e.g.ctrl+shift+m, ctrl+space) with controllable timeout.Includeshigh level API(e.g.recordandplay,add_abbreviation).Maps keys as they actually are in your layout, withfull internationalization support(e.g.Ctrl+ç).Events automatically captured in separate thread, doesn't block main program.Tested and documented.Doesn't break accented dead keys (I'm looking at you, pyHook).Mouse support available via projectmouse(pip install mouse).UsageInstall thePyPI package:pip install keyboardor clone the repository (no installation required, source files are sufficient):git clone https://github.com/boppreh/keyboardordownload and extract the zipinto your project folder.Then check theAPI docs belowto see what features are available.ExampleUse as library:importkeyboardkeyboard.press_and_release('shift+s, space')keyboard.write('The quick brown fox jumps over the lazy dog.')keyboard.add_hotkey('ctrl+shift+a',print,args=('triggered','hotkey'))# Press PAGE UP then PAGE DOWN to type "foobar".keyboard.add_hotkey('page up, page down',lambda:keyboard.write('foobar'))# Blocks until you press esc.keyboard.wait('esc')# Record events until 'esc' is pressed.recorded=keyboard.record(until='esc')# Then replay back at three times the speed.keyboard.play(recorded,speed_factor=3)# Type @@ then press space to replace with abbreviation.keyboard.add_abbreviation('@@','my.long.email@example.com')# Block forever, like `while True`.keyboard.wait()Use as standalone module:# Save JSON events to a file until interrupted:python-mkeyboard>events.txt

catevents.txt# {"event_type": "down", "scan_code": 25, "name": "p", "time": 1622447562.2994788, "is_keypad": false}# {"event_type": "up", "scan_code": 25, "name": "p", "time": 1622447562.431007, "is_keypad": false}# ...# Replay eventspython-mkeyboard<events.txtKnown limitations:Events generated under Windows don't report device id (event.device == None).#21Media keys on Linux may appear nameless (scan-code only) or not at all.#20Key suppression/blocking only available on Windows.#22To avoid depending on X, the Linux parts reads raw device files (/dev/input/input*) but this requires root.Other applications, such as some games, may register hooks that swallow all key events. In this casekeyboardwill be unable to report events.This program makes no attempt to hide itself, so don't use it for keyloggers or online gaming bots. Be responsible.SSH connections forward only the text typed, not keyboard events. Therefore if you connect to a server or Raspberry PI that is runningkeyboardvia SSH, the server will not detect your key events.Common patterns and mistakesPreventing the program from closingimportkeyboardkeyboard.add_hotkey('space',lambda:print('space was pressed!'))# If the program finishes, the hotkey is not in effect anymore.# Don't do this! This will use 100% of your CPU.#while True: pass# Use this insteadkeyboard.wait()# or thisimporttimewhileTrue:time.sleep(1000000)Waiting for a key press one timeimportkeyboard# Don't do this! This will use 100% of your CPU until you press the key.##while not keyboard.is_pressed('space'):#    continue#print('space was pressed, continuing...')# Do this insteadkeyboard.wait('space')print('space was pressed, continuing...')Repeatedly waiting for a key pressimportkeyboard# Don't do this!##while True:#    if keyboard.is_pressed('space'):#        print('space was pressed!')## This will use 100% of your CPU and print the message many times.# Do this insteadwhileTrue:keyboard.wait('space')print('space was pressed! Waiting on it again...')# or thiskeyboard.add_hotkey('space',lambda:print('space was pressed!'))keyboard.wait()Invoking code when an event happensimportkeyboard# Don't do this! This will call `print('space')` immediately then fail when the key is actually pressed.#keyboard.add_hotkey('space', print('space was pressed'))# Do this insteadkeyboard.add_hotkey('space',lambda:print('space was pressed'))# or thisdefon_space():print('space was pressed')keyboard.add_hotkey('space',on_space)# or thiswhileTrue:# Wait for the next event.event=keyboard.read_event()ifevent.event_type==keyboard.KEY_DOWNandevent.name=='space':print('space was pressed')'Press any key to continue'# Don't do this! The `keyboard` module is meant for global events, even when your program is not in focus.#import keyboard#print('Press any key to continue...')#keyboard.get_event()# Do this insteadinput('Press enter to continue...')# Or one of the suggestions from here# https://stackoverflow.com/questions/983354/how-to-make-a-script-wait-for-a-pressed-key