-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathShortcut.py
95 lines (73 loc) · 2.58 KB
/
Shortcut.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
import PyHook3
import pythoncom
class Shortcut():
global HOTKEYS
global ACTIONS
global KEY_STATUS
global PRESSED_COUNT
HOTKEYS = {}
ACTIONS = {}
KEY_STATUS = {}
PRESSED_COUNT = {}
def __init__(self):
pass
def add(self, key_group_id , key_group, action):
global HOTKEYS
global ACTIONS
HOTKEYS[key_group_id] = key_group
ACTIONS[key_group_id] = action
def clear(self):
HOTKEYS.clear()
ACTIONS.clear()
def KeyDownEvent(self, event):
global KEY_STATUS
global HOTKEYS
global PRESSED_COUNT
global ACTIONS
#标记状态
KEY_STATUS[event.KeyID] = True
# print('HOTKEYS: \n%s' % HOTKEYS)
# print('KEY DOWN/press key status: \n %s' % KEY_STATUS)
for action_id, key_group in HOTKEYS.items():
#检查每个按键状态
pressed = True
for i in range(len(key_group)):
key = key_group[i]
if key not in KEY_STATUS:
pressed = False
break
if pressed:
# if action_id not in PRESSED_COUNT.keys():
# PRESSED_COUNT[action_id] =1
# else:
# PRESSED_COUNT[action_id] +=1
print('match.')
#限制一直按下快捷键 动作执行的次数
if action_id not in PRESSED_COUNT.keys():
action=ACTIONS[action_id]
if action:
action()
PRESSED_COUNT[action_id] =1
return True
def KeyUpEvent(self, event):
global KEY_STATUS
global HOTKEYS
global PRESSED_COUNT
global ACTIONS
if event.KeyID in KEY_STATUS.keys():
KEY_STATUS.pop(event.KeyID)
# print('KEY UP/press key status: \n %s' % KEY_STATUS)
#清空组合按键记录
for action_id, key_group in HOTKEYS.items():
if event.KeyID in key_group:
if action_id in PRESSED_COUNT.keys():
PRESSED_COUNT.pop(action_id)
return True
def monitor(self):
# create the hook mananger
hm = PyHook3.HookManager()
# hm.MouseAllButtonsDown = OnMouseEvent
hm.KeyDown = self.KeyDownEvent
hm.KeyUp = self.KeyUpEvent
hm.HookKeyboard()
# pythoncom.PumpMessages()