-
Notifications
You must be signed in to change notification settings - Fork 1
/
KeyboardListener.py
executable file
·81 lines (62 loc) · 1.99 KB
/
KeyboardListener.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
import io
import time
import keyboard
from screen_shot import ScreenCapture
class KeyboardListener:
def __init__(self, tcpServer):
self.tcpServer = tcpServer
self.t = 0
self.c = 0
self.key_state_map={}
self.screen_capture = None
def listen_keyboard(self,callback):
self.callback = callback
keyboard.hook(self.onKeyEvent)
keyboard.wait()
def onImgCapture(self,pic):
imgByteArr = io.BytesIO()
pic.save(imgByteArr, format='JPEG')
bytes_data = imgByteArr.getvalue()
self.tcpServer.send_img(bytes_data)
def isCtrlHolding(self):
return ('ctrl' in self.key_state_map and self.key_state_map['ctrl']=='down')\
or ('left ctrl' in self.key_state_map and self.key_state_map['left ctrl']=='down')\
or ('right ctrl' in self.key_state_map and self.key_state_map['right ctrl']=='down')
def isAltHolding(self):
return ('alt' in self.key_state_map and self.key_state_map['alt']=='down')\
or ('left alt' in self.key_state_map and self.key_state_map['left alt']=='down')\
or ('right alt' in self.key_state_map and self.key_state_map['right alt']=='down')
def isKeyHolding(self,key):
return (key in self.key_state_map and self.key_state_map[key]=='down')
def onKeyEvent(self,key):
#update key_state_map
self.key_state_map[key.name.lower()]=key.event_type
#is screenshoot?
if self.isKeyHolding("caps lock")\
and key.event_type=="down"\
and key.name.lower()=="a":
self.screen_capture = ScreenCapture()
self.screen_capture.are_capture(self.onImgCapture)
print(self.key_state_map)
#is triple c?
if key.event_type=="down" \
and key.name.lower()=="c" \
and self.isCtrlHolding():
if self.t == 0:
self.t=time.time()
self.c += 1
print("wait for nex c",self.c)
return
if (time.time()-self.t<0.5):
self.t=time.time()
self.c += 1
print("wait for nex c:",self.c)
else:
self.c = 0
self.t=0
print("wait for nex c",self.c)
if self.c>=2:
self.c=0
print("need trans")
if self.callback:
self.callback()