-
Notifications
You must be signed in to change notification settings - Fork 47
/
key_input.py
81 lines (60 loc) · 2.38 KB
/
key_input.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
# adapted from https://github.com/Sentdex/pygta5/blob/master/getkeys.py
import win32api as wapi
import time
import ast
# this is a list of characters to check when pressing in key_check()
keyList = ["\b"]
for char in "TFGHXCMQqpPYUN":
keyList.append(char)
def key_check():
keys = []
for key in keyList:
# the ord() function returns an integer representing the Unicode character
# chr() goes opposite way
if wapi.GetAsyncKeyState(ord(key)):
keys.append(key)
# doesn't work to catch shift...
return keys
# https://stackoverflow.com/questions/3698635/getting-cursor-position-in-python
from ctypes import windll, Structure, c_long, byref
class POINT(Structure):
_fields_ = [("x", c_long), ("y", c_long)]
def mouse_check():
pt = POINT()
windll.user32.GetCursorPos(byref(pt))
return pt.x, pt.y
def mouse_l_click_check(previous_status):
held_down=0; clicked=0;
current_status = wapi.GetKeyState(0x01)
if current_status < 0:
held_down = 1 # held down click
elif current_status != previous_status and not previous_status < 0:
clicked = 1 # just tapped this
return current_status, clicked, held_down
def mouse_r_click_check(previous_status):
held_down=0; clicked=0;
current_status = wapi.GetKeyState(0x02)
if current_status < 0:
held_down = 1 # held down click
elif current_status != previous_status and not previous_status < 0:
clicked = 1 # just tapped this
return current_status, clicked, held_down
def mouse_log_test():
loop_fps=20
previous_status_l = wapi.GetKeyState(0x01)
previous_status_r = wapi.GetKeyState(0x02)
while True:
loop_start_time = time.time() # this is in seconds
current_status_l, clicked_l, held_down_l = mouse_l_click_check(previous_status_l)
current_status_r, clicked_r, held_down_r = mouse_r_click_check(previous_status_r)
print('l_click', clicked_l, ' l_held', held_down_l,
' | r_click', clicked_r, ' r_held', held_down_r)
previous_status_l = current_status_l
previous_status_r = current_status_r
# time.sleep(0.1)
# wait until end of time step
while time.time() < loop_start_time + 1/loop_fps:
pass
return
if __name__ == "__main__":
mouse_log_test()