forked from jacklam718/myDesktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input_event.py
276 lines (233 loc) · 7.19 KB
/
input_event.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
from os import sys
platform = sys.platform[:3]
if platform == 'lin':
from Xlib.display import Display
from Xlib import X
from Xlib.ext.xtest import fake_input
elif platform == 'mac':
from Quartz import *
from AppKit import NSEvent
pressID = [None, kCGEventLeftMothe,
kCGEventRightMouseDown, kCGEventOtherMouseDown]
releaseID = [None, kCGEventLeftMouseUp,
kCGEventRightMouseUp, kCGEventOtherMouseUp]
elif platform == 'win':
from ctypes import *
import win32api, win32con
class POINT(Structure):
_fields_ = [("x", c_ulong),
("y", c_ulong)]
button_ids = [None, 1, 3, 2, 4, 5, 6, 7]
KEY_BackSpace = 0xff08
KEY_Tab = 0xff09
KEY_Return = 0xff0d
KEY_Escape = 0xff1b
KEY_Insert = 0xff63
KEY_Delete = 0xffff
KEY_Home = 0xff50
KEY_End = 0xff57
KEY_PageUp = 0xff55
KEY_PageDown = 0xff56
KEY_Left = 0xff51
KEY_Up = 0xff52
KEY_Right = 0xff53
KEY_Down = 0xff54
KEY_F1 = 0xffbe
KEY_F2 = 0xffbf
KEY_F3 = 0xffc0
KEY_F4 = 0xffc1
KEY_F5 = 0xffc2
KEY_F6 = 0xffc3
KEY_F7 = 0xffc4
KEY_F8 = 0xffc5
KEY_F9 = 0xffc6
KEY_F10 = 0xffc7
KEY_F11 = 0xffc8
KEY_F12 = 0xffc9
KEY_F13 = 0xFFCA
KEY_F14 = 0xFFCB
KEY_F15 = 0xFFCC
KEY_F16 = 0xFFCD
KEY_F17 = 0xFFCE
KEY_F18 = 0xFFCF
KEY_F19 = 0xFFD0
KEY_F20 = 0xFFD1
KEY_ShiftLeft = 0xffe1
KEY_ShiftRight = 0xffe2
KEY_ControlLeft = 0xffe3
KEY_ControlRight = 0xffe4
KEY_MetaLeft = 0xffe7
KEY_MetaRight = 0xffe8
KEY_AltLeft = 0xffe9
KEY_AltRight = 0xffea
KEY_Scroll_Lock = 0xFF14
KEY_Sys_Req = 0xFF15
KEY_Num_Lock = 0xFF7F
KEY_Caps_Lock = 0xFFE5
KEY_Pause = 0xFF13
KEY_Super_L = 0xFFEB
KEY_Super_R = 0xFFEC
KEY_Hyper_L = 0xFFED
KEY_Hyper_R = 0xFFEE
keymap = {
16777219: KEY_BackSpace,
16777217: KEY_Tab,
16777220: KEY_Return,
16777216: KEY_Escape,
16777222: KEY_Insert,
16777223: KEY_Delete,
16777232: KEY_Home,
16777233: KEY_End,
16777238: KEY_PageUp,
16777239: KEY_PageDown,
16777234: KEY_Left,
16777235: KEY_Up,
16777236: KEY_Right,
16777237: KEY_Down,
16777264: KEY_F1,
16777265: KEY_F2,
16777266: KEY_F3,
16777267: KEY_F4,
16777268: KEY_F5,
16777269: KEY_F6,
16777270: KEY_F7,
16777271: KEY_F8,
16777272: KEY_F9,
16777273: KEY_F10,
16777274: KEY_F11,
16777275: KEY_F12,
16777276: KEY_F13,
16777277: KEY_F14,
16777278: KEY_F15,
16777279: KEY_F16,
16777280: KEY_F17,
16777281: KEY_F18,
16777282: KEY_F19,
16777283: KEY_F20,
16777254: KEY_Scroll_Lock,
16777226: KEY_Sys_Req,
16777253: KEY_Num_Lock,
16777252: KEY_Caps_Lock,
16777224: KEY_Pause,
16777299: KEY_Super_L,
16777300: KEY_Super_R,
16777302: KEY_Hyper_L,
16777303: KEY_Hyper_R}
#---------#
## Linux ##
#---------#
class x11_Mouse:
def __init__(self):
self.display = Display( )
def press(self, x, y, button=1):
self.move(x, y)
fake_input(self.display, X.ButtonPress, button_ids[button])
self.display.sync( )
def release(self, x, y, button=1):
self.move(x, y)
fake_input(self.display, X.ButtonRelease, button_ids[button])
self.display.sync( )
def move(self, x, y):
#if (x, y) != self.position( ):
fake_input(self.display, X.MotionNotify, x=x, y=y)
self.display.sync( )
def position(self):
pos = self.display.screen( ).root.query_pointer( )._data
return pos['root_x'], pos['root_y']
def screen_size(self):
width = self.display.screen( ).width_in_pixels
height = self.display.screen( ).height_in_pixels
return width, heightwe
class x11_Keyboard:
def __init__(self):
self.display = Display( )
def press(self, key):
if key >= 256:
keycode = self.display.keysym_to_keycode(keymap[key])
keycode = self.display.keysym_to_keycode(key)
fake_input(self.window(), X.KeyPress, keycode)
self.display.sync( )
def release(self, key):
if key >= 256:
keycode = self.display.keysym_to_keycode(keymap[key])
keycode = self.display.keysym_to_keycode(key)
fake_input(self.window(), X.KeyRelease, keycode)
self.display.sync( )
def window(self):
return self.display.get_input_focus( )._data['focus']
#------------#
## Mac OS X ##
#------------#
class PyMouse:
def press(self, x, y, button=1):
event = CGEventCreateMouseEvent(None,
pressID[button],
(x, y),
button - 1)
CGEventPost(kCGHIDEventTap, event)
def release(self, x, y, button=1):
event = CGEventCreateMouseEvent(None,
releaseID[button],
(x, y),
button - 1)
CGEventPost(kCGHIDEventTap, event)
def move(self, x, y):
move = CGEventCreateMouseEvent(None, kCGEventMouseMoved, (x, y), 0)
CGEventPost(kCGHIDEventTap, move)
def drag(self, x, y):
drag = CGEventCreateMouseEvent(None, kCGEventLeftMouseDragged, (x, y), 0)
CGEventPost(kCGHIDEventTap, drag)
def position(self):
loc = NSEvent.mouseLocation()
return loc.x, CGDisplayPixelsHigh(0) - loc.y
def screen_size(self):
return CGDisplayPixelsWide(0), CGDisplayPixelsHigh(0)
class mac_Keyboard:
def press(self, key):
pass
def release(self, key):
pass
def window(self):
pass
#-----------#
## Windows ##
#-----------#
class win_Mouse:
def press(self, x, y, button=1):
buttonAction = 2 ** ((2 * button) - 1)
self.move(x, y)
win32api.mouse_event(buttonAction, x, y)
def release(self, x, y, button=1):
buttonAction = 2 ** ((2 * button))
self.move(x, y)
win32api.mouse_event(buttonAction, x, y)
def move(self, x, y):
windll.user32.SetCursorPos(x, y)
def position(self):
pt = POINT()
windll.user32.GetCursorPos(byref(pt))
return pt.x, pt.y
def screen_size(self):
width = windll.user32.GetSystemMetrics(0)
height = windll.user32.GetSystemMetrics(1)
return width, height
class win_Keyboard:
def press(self, key):
pass
def release(self, key):
pass
def window(self):
pass
#----------------------------------------------------------------#
## inherit the appropriate category based on system type, ##
## so just needed import Mouse and Keyboard in our application, ##
#----------------------------------------------------------------#
if platform == 'lin':
class Mouse(x11_Mouse): pass
class Keyboard(x11_Keyboard): pass
elif platform == 'mac':
class Mouse(mac_Mouse):pass
class Keyboard(mac_Keyboard): pass
elif platform == 'win':
class Mouse(win_Mouse): pass
class Keyboard(win_Keyboard): pass