|
1 | | -# SimpleHotkeys |
2 | | -Python library for creating keyboard shortcuts 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 removes 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 | | -Currently no option to install via `pip`. |
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. Note that there is little reaction delay that depends on what callbacks are set ( |
78 | | -when only `callback_on_press` is set, the callback is called on keydown; |
79 | | -when `callback_on_longpress` or `callback_on_toolongpress` is set, it waits for the key release; |
80 | | -when `callback_on_doublepress` is set, waits for next keypress). |
81 | | -Calling this function for the second time with same key_list will remove the old callbacks. |
82 | | -To prevent blocking keypress detection, callbacks are always called as threads. |
83 | | - |
84 | | -```python |
85 | | -simplehotkeys.catch_hotkey(timeout=10) |
86 | | -``` |
87 | | -Waits for key combination being pressed and returns it as list of keys. If no key is pressed within the `timeout` nothing is returned. |
88 | | - |
89 | | -## Todos |
90 | | - - Use key strings list instead of list of pynput key objects. |
91 | | - - Add hotkey handler that will return keypress duration, keypress number, ... (customizable mode) |
92 | | - |
| 1 | +# SimpleHotkeys |
| 2 | +Python library for creating keyboard shortcuts based on [pynput](https://github.com/moses-palmer/pynput) and [keyboard](https://github.com/boppreh/keyboard) 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 removes 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 | +### Choosing whether to utilize [pynput](https://github.com/moses-palmer/pynput) or [keyboard](https://github.com/boppreh/keyboard) |
| 68 | +I ran into problems when using both libraries on Windows ([keyboard](https://github.com/boppreh/keyboard) misdetects `numpad *` as `print screen`; with [pynput](https://github.com/moses-palmer/pynput) hotkeys stops working after some time) so i decided to implement both. |
| 69 | +The choice of [pynput](https://github.com/moses-palmer/pynput) or [keyboard](https://github.com/boppreh/keyboard) depends on what is given as `keys` to `simplehotkeys.add_hotkey` method: |
| 70 | +```python |
| 71 | +simplehotkeys.add_hotkey(["print screen"], lambda: print("keyboard")) # use keyboard backend since keyboard accepts bare strings |
| 72 | + |
| 73 | +simplehotkeys.add_hotkey([keyboard2.Key.print_screen], lambda: print("pynput")) # use pynput backend |
| 74 | +``` |
| 75 | + |
| 76 | + |
| 77 | +## Instalation |
| 78 | +Currently no option to install via `pip`. |
| 79 | +Simply add the `simplehotkeys.py` to your project directory. |
| 80 | + |
| 81 | + |
| 82 | +## Library methods |
| 83 | + |
| 84 | +```python |
| 85 | +simplehotkeys.add_hotkey(keys_list, callback_on_press=None, callback_on_longpress=None, callback_on_toolongpress=None, callback_on_doublepress=None) |
| 86 | +``` |
| 87 | +Sets up callbacks for given key combination. Note that there is little reaction delay that depends on what callbacks are set ( |
| 88 | +when only `callback_on_press` is set, the callback is called on keydown; |
| 89 | +when `callback_on_longpress` or `callback_on_toolongpress` is set, it waits for the key release; |
| 90 | +when `callback_on_doublepress` is set, waits for next keypress). |
| 91 | +Calling this function for the second time with same key_list will remove the old callbacks. |
| 92 | +To prevent blocking keypress detection, callbacks are always called as threads. |
| 93 | + |
| 94 | +```python |
| 95 | +simplehotkeys.catch_hotkey(timeout=10) |
| 96 | +``` |
| 97 | +Waits for key combination being pressed and returns it as list of keys. If no key is pressed within the `timeout` nothing is returned. |
| 98 | + |
| 99 | +## Todos |
| 100 | + - Use key strings list instead of list of pynput key objects. |
| 101 | + - Add hotkey handler that will return keypress duration, keypress number, ... (customizable mode) |
0 commit comments