-
Notifications
You must be signed in to change notification settings - Fork 1
/
hot_keys.py
104 lines (76 loc) · 2.02 KB
/
hot_keys.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
"""
from pynput.keyboard import Listener, Key, Controller
#"l": [dd]
isPrint = True
hot_keys = {
Key.
Key.page_up: [Key.up, Key.down, Key.right, Key.left, Key.up]
}
keyboard = Controller()
def press_key(key):
for i in hot_keys[key]:
keyboard.press(i)
def on_press(key):
if isPrint:
print(key)
#if k == "\x03":
# exit()
if key in hot_keys.keys():
press_key(key)
try:
if key.char in hot_keys.keys():
press_key(key.char)
except Exception:
pass
with Listener(on_press=on_press) as listener:
listener.join()
"""
import tkinter as tk
from pynput.keyboard import Listener, Key, Controller
isPrint = True
running = False
hot_keys = {
"l": ["o"],
Key.page_up: [Key.up, Key.down, Key.right, Key.left, Key.up]
}
keyboard = Controller()
def press_key(key):
for i in hot_keys[key]:
keyboard.press(i)
def on_press(key):
global running
if running:
if isPrint:
key_label.config(text=str(key))
if key in hot_keys.keys():
press_key(key)
try:
if key.char in hot_keys.keys():
press_key(key.char)
except AttributeError:
pass
def start_listener():
global running
if not running:
listener = Listener(on_press=on_press)
listener.start()
running = True
status_label.config(text="Running")
def stop_listener():
global running
if running:
running = False
status_label.config(text="Stopped")
# GUI setup
root = tk.Tk()
root.title("Keyboard Listener")
root.geometry("300x150")
start_button = tk.Button(root, text="Start", command=start_listener)
start_button.pack(pady=10)
stop_button = tk.Button(root, text="Stop", command=stop_listener)
stop_button.pack(pady=5)
status_label = tk.Label(root, text="Stopped")
status_label.pack(pady=5)
key_label = tk.Label(root, text="")
key_label.pack(pady=5)
root.mainloop()