|
1 | 1 | # SimpleHotkeys |
2 | | -Python library for creating system-wide keyboard shortcusts |
| 2 | +Python library for creating keyboard shortcusts based on [pynput](https://github.com/moses-palmer/pynput) library. |
| 3 | + |
| 4 | +## Usage |
| 5 | + |
| 6 | +### Simple |
| 7 | +```python |
| 8 | +import simplehotkeys |
| 9 | + |
| 10 | +number = 0 |
| 11 | +def do_something (): |
| 12 | + global number |
| 13 | + number+=1 |
| 14 | + print("key press number", number) |
| 15 | + |
| 16 | +simplehotkeys.add_hotkey([simplehotkeys.pynput.keyboard.Key.ctrl_l, simplehotkeys.pynput.keyboard.Key.alt_l], do_something) |
| 17 | +input() |
| 18 | +``` |
| 19 | + |
| 20 | +Prints message every time you press `left ctrl` and `left alt` key. |
| 21 | +``` |
| 22 | +key press number 1 |
| 23 | +key press number 2 |
| 24 | +key press number 3 |
| 25 | +key press number 4 |
| 26 | +key press number 5 |
| 27 | +key press number 6 |
| 28 | +key press number 7 |
| 29 | +... |
| 30 | +``` |
| 31 | + |
| 32 | + |
| 33 | +### More advanced |
| 34 | +```python |
| 35 | +import simplehotkeys |
| 36 | +import time |
| 37 | + |
| 38 | +time.sleep(1) |
| 39 | +print ("now press your desired hotkey") |
| 40 | +keys = simplehotkeys.catch_hotkey() |
| 41 | +print("\t", keys) |
| 42 | + |
| 43 | +simplehotkeys.add_hotkey(keys, |
| 44 | + lambda: print("single press"), |
| 45 | + lambda: print("long press"), |
| 46 | + lambda: print("too long press"), |
| 47 | + lambda: print("double press") |
| 48 | + ) |
| 49 | + |
| 50 | +time.sleep(30) |
| 51 | + |
| 52 | +simplehotkeys.add_hotkey(keys) |
| 53 | +input("from now no keypresses are handled") |
| 54 | +``` |
| 55 | +Inputs key combination and for 30 seconds react to its events, then remove all the callbacks. |
| 56 | + |
| 57 | +``` |
| 58 | +now press your desired hotkey |
| 59 | + [<Key.caps_lock: <20>>] |
| 60 | +single press |
| 61 | +double press |
| 62 | +long press |
| 63 | +too long press |
| 64 | +from now no keypresses are handled |
| 65 | +``` |
| 66 | + |
| 67 | +## Instalation |
| 68 | +No option to install via `pip` yet. |
| 69 | +Simply add the `simplehotkeys.py` to your project directory. |
| 70 | + |
| 71 | + |
| 72 | +## Library methods |
| 73 | + |
| 74 | +```python |
| 75 | +simplehotkeys.add_hotkey(keys_list, callback_on_press=None, callback_on_longpress=None, callback_on_toolongpress=None, callback_on_doublepress=None) |
| 76 | +``` |
| 77 | +Sets up callbacks for given key combination and event. Note that there is little reaction delay that depends on what callbacks are set (when only `callback_on_press` is set calls callback on keydown, when `callback_on_longpress` or `callback_on_toolongpress` is set waits for the key release, when `callback_on_doublepress` is set waits for next keypress). |
| 78 | +Calling this function for the second time with same key_list will remove the old callbacks. |
| 79 | +To prevent blocking keypress detection, callbacks are always called as threads. |
| 80 | + |
| 81 | +```python |
| 82 | +simplehotkeys.catch_hotkey(timeout=10) |
| 83 | +``` |
| 84 | +Waits for key combination being pressed and returns it as list of keys. If no key is pressed within the `timeout` nothing is returned. |
| 85 | + |
| 86 | +## Todos |
| 87 | + - Use key strings list instead of list of pynput key objects. |
| 88 | + - Add hotkey handler that will return keypress duration, keypress number, ... (customizable mode) |
| 89 | + |
0 commit comments