forked from iCafeCodeProject/iCafeAutomationTool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvirtual_input_service.py
159 lines (141 loc) · 4.08 KB
/
virtual_input_service.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
158
159
from ctypes import windll
import string
VkCode = {
"back": 0x08,
"tab": 0x09,
"return": 0x0D,
"shift": 0x10,
"control": 0x11,
"menu": 0x12,
"pause": 0x13,
"capital": 0x14,
"escape": 0x1B,
"space": 0x20,
"end": 0x23,
"home": 0x24,
"left": 0x25,
"up": 0x26,
"right": 0x27,
"down": 0x28,
"print": 0x2A,
"snapshot": 0x2C,
"insert": 0x2D,
"delete": 0x2E,
"lwin": 0x5B,
"rwin": 0x5C,
"numpad0": 0x60,
"numpad1": 0x61,
"numpad2": 0x62,
"numpad3": 0x63,
"numpad4": 0x64,
"numpad5": 0x65,
"numpad6": 0x66,
"numpad7": 0x67,
"numpad8": 0x68,
"numpad9": 0x69,
"multiply": 0x6A,
"add": 0x6B,
"separator": 0x6C,
"subtract": 0x6D,
"decimal": 0x6E,
"divide": 0x6F,
"f1": 0x70,
"f2": 0x71,
"f3": 0x72,
"f4": 0x73,
"f5": 0x74,
"f6": 0x75,
"f7": 0x76,
"f8": 0x77,
"f9": 0x78,
"f10": 0x79,
"f11": 0x7A,
"f12": 0x7B,
"numlock": 0x90,
"scroll": 0x91,
"lshift": 0xA0,
"rshift": 0xA1,
"lcontrol": 0xA2,
"rcontrol": 0xA3,
"lmenu": 0xA4,
"rmenu": 0XA5
}
MsgID = {
'press': 0x100,
'release': 0x101,
'mouse_move_': 0x0200,
'mouse_left_press': 0x0201,
'mouse_left_release': 0x202,
}
PostMessageW = windll.user32.PostMessageW
MapVirtualKeyW = windll.user32.MapVirtualKeyW
VkKeyScanA = windll.user32.VkKeyScanA
def get_window_handle(window_name, ui):
handle = windll.user32.FindWindowW(None, window_name)
if handle == 0:
msg = str("[input]:Target window not found!: %s" % window_name)
ui.msg_print(msg)
print("[input]:Target window not found!: %s" % window_name)
return -1
return handle
def get_vk_code(key):
if len(key) == 1 and key in string.printable:
return VkKeyScanA(ord(key))
else:
return VkCode.get(key)
def key_strike_generator(key, movement, input_mode, window_name, ui, pos_x, pos_y):
func = supported_input_methods.get(input_mode, "Invalid mode")
if func == 'Invalid mode':
msg = str("[Input]: Invalid input mode: %s" % input_mode)
ui.msg_print(msg)
print("[Input]: Invalid input mode: %s" % input_mode)
else:
handle = get_window_handle(window_name, ui)
if handle == -1:
msg = str("[input]:generate input failed!")
ui.msg_print(msg)
print("[input]:generate input failed!")
return -1
return func(key, movement, handle, pos_x, pos_y)
def post_message(key, movement, handle, pos_x, pos_y):
if key == 0:
return 0
elif key.find('mouse') != -1:
hwnd = handle
msg = MsgID.get(str(key + '_'+movement))
wparam = 0
pos_x = int(pos_x)
pos_y = int(pos_y)
lparam = pos_y << 16 | pos_x
PostMessageW(hwnd, msg, wparam, lparam)
return 0
else:
hwnd = handle
msg = MsgID.get(movement)
vk_code = get_vk_code(key)
scan_code = MapVirtualKeyW(vk_code, 0)
wparam = vk_code
lparam = 0
# lparam:
# 0 - 15(16 bit): key repeat count
# 16 - 23(8 bit): scan code
# 24: extended flag
# 25 - 28(4 bit): reserved
# 29: context code, if ALT is pressed, 1 for pressed
# 30: previous key state, 1 pressed, 0 released
# 31: transaction state flag, 1 release, 0 press
if msg == 0x100:
lparam = (scan_code << 16) | 1 # set repeated count as 1
elif msg == 0x101:
lparam = (scan_code << 16) | 0xC0000001 # set previous key state and transaction state flag as 1
PostMessageW(hwnd, msg, wparam, lparam)
return 0
def hook(key, movement, handle):
return 0
def hw_simulator(key, movement, handle):
return 0
supported_input_methods = {
'post_message': post_message,
'hook': hook,
'hw_simulator': hw_simulator
}