-
Notifications
You must be signed in to change notification settings - Fork 0
/
triggerKeyboard.py
157 lines (137 loc) · 4.26 KB
/
triggerKeyboard.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
from pykeyboard import PyKeyboardEvent
import time
from threading import Thread
"""
shuoGG: 本模块只公开key_trigger接口(装饰器)
eg:
@key_trigger('ctrl+shift+alt+f8', True) 具体见main函数
"""
SCREEN_SHOT_SHORTCUT = 'alt'
def key_trigger(key_str, is_trigger_once=True):
"""
@装饰器key_trigger
:param key_str: 快捷键
:param is_trigger_once: 是否只触发一次
:return:
"""
def new_func(func):
def decorate(*args, **kwargs):
keyboard_event_thread = Thread(target=__run_keyboard_task, args=(key_str,))
keyboard_event_thread.setDaemon(is_trigger_once) # setDaemon(True) --> 主线程挂了则t1跟着挂
keyboard_event_thread.start()
global is_key_triggered
while True:
if is_key_triggered:
is_key_triggered = False
func(*args, **kwargs)
if is_trigger_once:
break
time.sleep(0.01)
return decorate
return new_func
# 用于通知
is_key_triggered = False
def __run_keyboard_task(key_str):
"""
异步任务: 跑键盘监听任务
:param key_str:
:return:
"""
if TriggerKeyboardEvent.check_key_str(key_str) is False:
raise NameError('快捷键格式错误')
p = TriggerKeyboardEvent(key_str)
p.run()
class TriggerKeyboardEvent(PyKeyboardEvent):
def __init__(self, key_str=SCREEN_SHOT_SHORTCUT):
PyKeyboardEvent.__init__(self)
self.key_map = TriggerKeyboardEvent.create_key_map(TriggerKeyboardEvent.parse_full_key_str(key_str))
self.is_trigger = False
def run(self):
super().run()
def tap(self, keycode, character, press):
"""
@Override
:param keycode:
:param character:
:param press:
:return:
"""
global is_key_triggered
# print(keycode, character, press)
character = TriggerKeyboardEvent.to_standard_str(character)
if character in self.key_map:
self.key_map[character] = press
if TriggerKeyboardEvent.check_all_key_pressed(self.key_map):
is_key_triggered = True
@staticmethod
def parse_full_key_str(key_str):
"""
快捷键串分解成列表
:param key_str:
:return: list
"""
keys = list(map(lambda x: x.strip(), key_str.split('+')))
return keys
@staticmethod
def create_key_map(key_list):
"""
建立按键辅助表: key为键盘某按钮字符串 value为初始均为False
:param key_list:
:return: dict
"""
key_map = dict()
for key in key_list:
key_map[key] = False
return key_map
@staticmethod
def check_all_key_pressed(key_map):
"""
检查键盘辅助表所有项是否均为True
:param key_map:
:return: bool
"""
for key in key_map.keys():
if key_map[key] is False:
return False
return True
@staticmethod
def to_standard_str(character):
"""
tap回调传进来的character格式不标准,需要转一下
:param character:
:return: str
"""
if character.find('CONTROL') > -1:
return 'ctrl'
if character.find('MENU') > -1:
return 'alt'
if character.find('SHIFT') > -1:
return 'shift'
return character.lower()
@staticmethod
def check_key_str(key_str):
"""
检验输入的快捷键格式是否合法
:param key_str: eg: 'ctrl+shift+f1'
:return: bool
"""
key_list = TriggerKeyboardEvent.parse_full_key_str(key_str)
sp_key_count = 0
if len(key_list) < 1:
return False
for key in key_list:
if key.lower() == 'ctrl':
sp_key_count += 1
if key.lower() == 'alt':
sp_key_count += 1
if key.lower() == 'shift':
sp_key_count += 1
if len(key_list) - sp_key_count > 1:
return False
else:
return True
if __name__ == '__main__':
@key_trigger('ctrl+shift+alt+f9', False)
def test():
print('shuoGG')
test()