Skip to content

Commit cbd85dd

Browse files
authored
option to choose between pynput and keyboard module
1 parent f8b881c commit cbd85dd

File tree

2 files changed

+112
-97
lines changed

2 files changed

+112
-97
lines changed

README.md

Lines changed: 101 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,101 @@
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)

simplehotkeys.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
DOUBLEPRESS_BETWEEN = 300/1000
77
CALL_THREADED = True
88

9+
# (TO CHOOSE BETWEEN pynput AND keyboard EDIT "def pressed ()" AND CHECK WHICH KEY FORMAT IS USED IN YOUR CODE (string VS pynput.Key)) - NOW AUTODETECTED (depends on whether you give <class 'str> or <enum 'Key'>)
910

1011
## pynput ##
1112
import pynput.keyboard
@@ -40,8 +41,8 @@ def pynput_pressed_exclusively(keys=[]): # are exactly these keys pressed? othe
4041
return False
4142
## pynput ##
4243

43-
'''
44-
## keyboard ## - keyboard library can be also used (but was removed because contained bugs)
44+
45+
## keyboard ## - keyboard library can be also used (but was removed because contained bugs) - https://github.com/boppreh/keyboard/issues/41
4546
import keyboard
4647

4748
def keyboard_pressed(keys=[]): ## IF PRESSED + DONT CARE ABOUT OTHER KEYS => ITS NOT EXCLUSIVELY PRESSED
@@ -51,14 +52,19 @@ def keyboard_pressed(keys=[]): ## IF PRESSED + DONT CARE ABOUT OTHER KEYS => ITS
5152
return True
5253

5354
## keyboard ##
54-
'''
55+
5556

5657

5758

5859
def pressed (keys=[]):
5960
# TODO: convert keys=<string list> to pynput key list
60-
#return keyboard_pressed(keys)
61-
return pynput_pressed_exclusively(keys)
61+
if type(keys[0]) == str:
62+
return keyboard_pressed(keys)
63+
elif type(keys[0]) == pynput.keyboard.Key:
64+
return pynput_pressed_exclusively(keys)
65+
else:
66+
print ('simplehotkeys: ERROR key list "'+keys+'" is not in valid format')
67+
return False
6268

6369

6470

0 commit comments

Comments
 (0)