-
Notifications
You must be signed in to change notification settings - Fork 88
/
__init__.py
570 lines (460 loc) · 17.9 KB
/
__init__.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
import ctypes
import functools
import inspect
import time
SendInput = ctypes.windll.user32.SendInput
MapVirtualKey = ctypes.windll.user32.MapVirtualKeyW
# Constants for failsafe check and pause
FAILSAFE = True
FAILSAFE_POINTS = [(0, 0)]
PAUSE = 0.1 # Tenth-second pause by default.
# Constants for the mouse button names
LEFT = "left"
MIDDLE = "middle"
RIGHT = "right"
PRIMARY = "primary"
SECONDARY = "secondary"
# Mouse Scan Code Mappings
MOUSEEVENTF_MOVE = 0x0001
MOUSEEVENTF_ABSOLUTE = 0x8000
MOUSEEVENTF_LEFTDOWN = 0x0002
MOUSEEVENTF_LEFTUP = 0x0004
MOUSEEVENTF_LEFTCLICK = MOUSEEVENTF_LEFTDOWN + MOUSEEVENTF_LEFTUP
MOUSEEVENTF_RIGHTDOWN = 0x0008
MOUSEEVENTF_RIGHTUP = 0x0010
MOUSEEVENTF_RIGHTCLICK = MOUSEEVENTF_RIGHTDOWN + MOUSEEVENTF_RIGHTUP
MOUSEEVENTF_MIDDLEDOWN = 0x0020
MOUSEEVENTF_MIDDLEUP = 0x0040
MOUSEEVENTF_MIDDLECLICK = MOUSEEVENTF_MIDDLEDOWN + MOUSEEVENTF_MIDDLEUP
# KeyBdInput Flags
KEYEVENTF_EXTENDEDKEY = 0x0001
KEYEVENTF_KEYUP = 0x0002
KEYEVENTF_SCANCODE = 0x0008
KEYEVENTF_UNICODE = 0x0004
# MapVirtualKey Map Types
MAPVK_VK_TO_CHAR = 2
MAPVK_VK_TO_VSC = 0
MAPVK_VSC_TO_VK = 1
MAPVK_VSC_TO_VK_EX = 3
# Keyboard Scan Code Mappings
KEYBOARD_MAPPING = {
'escape': 0x01,
'esc': 0x01,
'f1': 0x3B,
'f2': 0x3C,
'f3': 0x3D,
'f4': 0x3E,
'f5': 0x3F,
'f6': 0x40,
'f7': 0x41,
'f8': 0x42,
'f9': 0x43,
'f10': 0x44,
'f11': 0x57,
'f12': 0x58,
'printscreen': 0xB7,
'prntscrn': 0xB7,
'prtsc': 0xB7,
'prtscr': 0xB7,
'scrolllock': 0x46,
'pause': 0xC5,
'`': 0x29,
'1': 0x02,
'2': 0x03,
'3': 0x04,
'4': 0x05,
'5': 0x06,
'6': 0x07,
'7': 0x08,
'8': 0x09,
'9': 0x0A,
'0': 0x0B,
'-': 0x0C,
'=': 0x0D,
'backspace': 0x0E,
'insert': 0xD2 + 1024,
'home': 0xC7 + 1024,
'pageup': 0xC9 + 1024,
'pagedown': 0xD1 + 1024,
# numpad
'numlock': 0x45,
'divide': 0xB5 + 1024,
'multiply': 0x37,
'subtract': 0x4A,
'add': 0x4E,
'decimal': 0x53,
'numpadenter': 0x9C + 1024,
'numpad1': 0x4F,
'numpad2': 0x50,
'numpad3': 0x51,
'numpad4': 0x4B,
'numpad5': 0x4C,
'numpad6': 0x4D,
'numpad7': 0x47,
'numpad8': 0x48,
'numpad9': 0x49,
'numpad0': 0x52,
# end numpad
'tab': 0x0F,
'q': 0x10,
'w': 0x11,
'e': 0x12,
'r': 0x13,
't': 0x14,
'y': 0x15,
'u': 0x16,
'i': 0x17,
'o': 0x18,
'p': 0x19,
'[': 0x1A,
']': 0x1B,
'\\': 0x2B,
'del': 0xD3 + 1024,
'delete': 0xD3 + 1024,
'end': 0xCF + 1024,
'capslock': 0x3A,
'a': 0x1E,
's': 0x1F,
'd': 0x20,
'f': 0x21,
'g': 0x22,
'h': 0x23,
'j': 0x24,
'k': 0x25,
'l': 0x26,
';': 0x27,
"'": 0x28,
'enter': 0x1C,
'return': 0x1C,
'shift': 0x2A,
'shiftleft': 0x2A,
'z': 0x2C,
'x': 0x2D,
'c': 0x2E,
'v': 0x2F,
'b': 0x30,
'n': 0x31,
'm': 0x32,
',': 0x33,
'.': 0x34,
'/': 0x35,
'shiftright': 0x36,
'ctrl': 0x1D,
'ctrlleft': 0x1D,
'win': 0xDB + 1024,
'winleft': 0xDB + 1024,
'alt': 0x38,
'altleft': 0x38,
' ': 0x39,
'space': 0x39,
'altright': 0xB8 + 1024,
'winright': 0xDC + 1024,
'apps': 0xDD + 1024,
'ctrlright': 0x9D + 1024,
# arrow key scancodes can be different depending on the hardware,
# so I think the best solution is to look it up based on the virtual key
# https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-mapvirtualkeya?redirectedfrom=MSDN
'up': MapVirtualKey(0x26, MAPVK_VK_TO_VSC),
'left': MapVirtualKey(0x25, MAPVK_VK_TO_VSC),
'down': MapVirtualKey(0x28, MAPVK_VK_TO_VSC),
'right': MapVirtualKey(0x27, MAPVK_VK_TO_VSC),
}
# C struct redefinitions
PUL = ctypes.POINTER(ctypes.c_ulong)
class KeyBdInput(ctypes.Structure):
_fields_ = [("wVk", ctypes.c_ushort),
("wScan", ctypes.c_ushort),
("dwFlags", ctypes.c_ulong),
("time", ctypes.c_ulong),
("dwExtraInfo", PUL)]
class HardwareInput(ctypes.Structure):
_fields_ = [("uMsg", ctypes.c_ulong),
("wParamL", ctypes.c_short),
("wParamH", ctypes.c_ushort)]
class MouseInput(ctypes.Structure):
_fields_ = [("dx", ctypes.c_long),
("dy", ctypes.c_long),
("mouseData", ctypes.c_ulong),
("dwFlags", ctypes.c_ulong),
("time", ctypes.c_ulong),
("dwExtraInfo", PUL)]
class POINT(ctypes.Structure):
_fields_ = [("x", ctypes.c_long),
("y", ctypes.c_long)]
class Input_I(ctypes.Union):
_fields_ = [("ki", KeyBdInput),
("mi", MouseInput),
("hi", HardwareInput)]
class Input(ctypes.Structure):
_fields_ = [("type", ctypes.c_ulong),
("ii", Input_I)]
# Fail Safe and Pause implementation
class FailSafeException(Exception):
pass
def failSafeCheck():
if FAILSAFE and tuple(position()) in FAILSAFE_POINTS:
raise FailSafeException(
"PyDirectInput fail-safe triggered from mouse moving to a corner of the screen. To disable this " \
"fail-safe, set pydirectinput.FAILSAFE to False. DISABLING FAIL-SAFE IS NOT RECOMMENDED."
)
def _handlePause(_pause):
if _pause:
assert isinstance(PAUSE, int) or isinstance(PAUSE, float)
time.sleep(PAUSE)
# direct copy of _genericPyAutoGUIChecks()
def _genericPyDirectInputChecks(wrappedFunction):
@functools.wraps(wrappedFunction)
def wrapper(*args, **kwargs):
funcArgs = inspect.getcallargs(wrappedFunction, *args, **kwargs)
failSafeCheck()
returnVal = wrappedFunction(*args, **kwargs)
_handlePause(funcArgs.get("_pause"))
return returnVal
return wrapper
# Helper Functions
def _to_windows_coordinates(x=0, y=0):
display_width, display_height = size()
# the +1 here prevents exactly mouse movements from sometimes ending up off by 1 pixel
windows_x = (x * 65536) // display_width + 1
windows_y = (y * 65536) // display_height + 1
return windows_x, windows_y
# position() works exactly the same as PyAutoGUI. I've duplicated it here so that moveRel() can use it to calculate
# relative mouse positions.
def position(x=None, y=None):
cursor = POINT()
ctypes.windll.user32.GetCursorPos(ctypes.byref(cursor))
return (x if x else cursor.x, y if y else cursor.y)
# size() works exactly the same as PyAutoGUI. I've duplicated it here so that _to_windows_coordinates() can use it
# to calculate the window size.
def size():
return (ctypes.windll.user32.GetSystemMetrics(0), ctypes.windll.user32.GetSystemMetrics(1))
# Main Mouse Functions
# Ignored parameters: duration, tween, logScreenshot
@_genericPyDirectInputChecks
def mouseDown(x=None, y=None, button=PRIMARY, duration=None, tween=None, logScreenshot=None, _pause=True):
if not x is None or not y is None:
moveTo(x, y)
ev = None
if button == PRIMARY or button == LEFT:
ev = MOUSEEVENTF_LEFTDOWN
elif button == MIDDLE:
ev = MOUSEEVENTF_MIDDLEDOWN
elif button == SECONDARY or button == RIGHT:
ev = MOUSEEVENTF_RIGHTDOWN
if not ev:
raise ValueError('button arg to _click() must be one of "left", "middle", or "right", not %s' % button)
extra = ctypes.c_ulong(0)
ii_ = Input_I()
ii_.mi = MouseInput(0, 0, 0, ev, 0, ctypes.pointer(extra))
x = Input(ctypes.c_ulong(0), ii_)
SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))
# Ignored parameters: duration, tween, logScreenshot
@_genericPyDirectInputChecks
def mouseUp(x=None, y=None, button=PRIMARY, duration=None, tween=None, logScreenshot=None, _pause=True):
if not x is None or not y is None:
moveTo(x, y)
ev = None
if button == PRIMARY or button == LEFT:
ev = MOUSEEVENTF_LEFTUP
elif button == MIDDLE:
ev = MOUSEEVENTF_MIDDLEUP
elif button == SECONDARY or button == RIGHT:
ev = MOUSEEVENTF_RIGHTUP
if not ev:
raise ValueError('button arg to _click() must be one of "left", "middle", or "right", not %s' % button)
extra = ctypes.c_ulong(0)
ii_ = Input_I()
ii_.mi = MouseInput(0, 0, 0, ev, 0, ctypes.pointer(extra))
x = Input(ctypes.c_ulong(0), ii_)
SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))
# Ignored parameters: duration, tween, logScreenshot
@_genericPyDirectInputChecks
def click(x=None, y=None, clicks=1, interval=0.0, button=PRIMARY, duration=None, tween=None, logScreenshot=None,
_pause=True):
if not x is None or not y is None:
moveTo(x, y)
ev = None
if button == PRIMARY or button == LEFT:
ev = MOUSEEVENTF_LEFTCLICK
elif button == MIDDLE:
ev = MOUSEEVENTF_MIDDLECLICK
elif button == SECONDARY or button == RIGHT:
ev = MOUSEEVENTF_RIGHTCLICK
if not ev:
raise ValueError('button arg to _click() must be one of "left", "middle", or "right", not %s' % button)
for i in range(clicks):
failSafeCheck()
extra = ctypes.c_ulong(0)
ii_ = Input_I()
ii_.mi = MouseInput(0, 0, 0, ev, 0, ctypes.pointer(extra))
x = Input(ctypes.c_ulong(0), ii_)
SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))
time.sleep(interval)
def leftClick(x=None, y=None, interval=0.0, duration=0.0, tween=None, logScreenshot=None, _pause=True):
click(x, y, 1, interval, LEFT, duration, tween, logScreenshot, _pause)
def rightClick(x=None, y=None, interval=0.0, duration=0.0, tween=None, logScreenshot=None, _pause=True):
click(x, y, 1, interval, RIGHT, duration, tween, logScreenshot, _pause)
def middleClick(x=None, y=None, interval=0.0, duration=0.0, tween=None, logScreenshot=None, _pause=True):
click(x, y, 1, interval, MIDDLE, duration, tween, logScreenshot, _pause)
def doubleClick(x=None, y=None, interval=0.0, button=LEFT, duration=0.0, tween=None, logScreenshot=None, _pause=True):
click(x, y, 2, interval, button, duration, tween, logScreenshot, _pause)
def tripleClick(x=None, y=None, interval=0.0, button=LEFT, duration=0.0, tween=None, logScreenshot=None, _pause=True):
click(x, y, 3, interval, button, duration, tween, logScreenshot, _pause)
# Missing feature: scroll functions
# Ignored parameters: duration, tween, logScreenshot
# PyAutoGUI uses ctypes.windll.user32.SetCursorPos(x, y) for this, which might still work fine in DirectInput
# environments.
# Use the relative flag to do a raw win32 api relative movement call (no MOUSEEVENTF_ABSOLUTE flag), which may be more
# appropriate for some applications. Note that this may produce inexact results depending on mouse movement speed.
@_genericPyDirectInputChecks
def moveTo(x=None, y=None, duration=None, tween=None, logScreenshot=False, _pause=True, relative=False):
if not relative:
x, y = position(x, y) # if only x or y is provided, will keep the current position for the other axis
x, y = _to_windows_coordinates(x, y)
extra = ctypes.c_ulong(0)
ii_ = Input_I()
ii_.mi = MouseInput(x, y, 0, (MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE), 0, ctypes.pointer(extra))
command = Input(ctypes.c_ulong(0), ii_)
SendInput(1, ctypes.pointer(command), ctypes.sizeof(command))
else:
currentX, currentY = position()
moveRel(x - currentX, y - currentY, relative=True)
# Ignored parameters: duration, tween, logScreenshot
# move() and moveRel() are equivalent.
# Use the relative flag to do a raw win32 api relative movement call (no MOUSEEVENTF_ABSOLUTE flag), which may be more
# appropriate for some applications.
@_genericPyDirectInputChecks
def moveRel(xOffset=None, yOffset=None, duration=None, tween=None, logScreenshot=False, _pause=True, relative=False):
if not relative:
x, y = position()
if xOffset is None:
xOffset = 0
if yOffset is None:
yOffset = 0
moveTo(x + xOffset, y + yOffset)
else:
# When using MOUSEEVENTF_MOVE for relative movement the results may be inconsistent.
# "Relative mouse motion is subject to the effects of the mouse speed and the two-mouse threshold values. A user
# sets these three values with the Pointer Speed slider of the Control Panel's Mouse Properties sheet. You can
# obtain and set these values using the SystemParametersInfo function."
# https://docs.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-mouseinput
# https://stackoverflow.com/questions/50601200/pyhon-directinput-mouse-relative-moving-act-not-as-expected
extra = ctypes.c_ulong(0)
ii_ = Input_I()
ii_.mi = MouseInput(xOffset, yOffset, 0, MOUSEEVENTF_MOVE, 0, ctypes.pointer(extra))
command = Input(ctypes.c_ulong(0), ii_)
SendInput(1, ctypes.pointer(command), ctypes.sizeof(command))
move = moveRel
# Missing feature: drag functions
# Keyboard Functions
# Ignored parameters: logScreenshot
# Missing feature: auto shift for special characters (ie. '!', '@', '#'...)
@_genericPyDirectInputChecks
def keyDown(key, logScreenshot=None, _pause=True):
if not key in KEYBOARD_MAPPING or KEYBOARD_MAPPING[key] is None:
return
keybdFlags = KEYEVENTF_SCANCODE
# Init event tracking
insertedEvents = 0
expectedEvents = 1
# arrow keys need the extended key flag
if key in ['up', 'left', 'down', 'right']:
keybdFlags |= KEYEVENTF_EXTENDEDKEY
# if numlock is on and an arrow key is being pressed, we need to send an additional scancode
# https://stackoverflow.com/questions/14026496/sendinput-sends-num8-when-i-want-to-send-vk-up-how-come
# https://handmade.network/wiki/2823-keyboard_inputs_-_scancodes,_raw_input,_text_input,_key_names
if ctypes.windll.user32.GetKeyState(0x90):
# We need to press two keys, so we expect to have inserted 2 events when done
expectedEvents = 2
hexKeyCode = 0xE0
extra = ctypes.c_ulong(0)
ii_ = Input_I()
ii_.ki = KeyBdInput(0, hexKeyCode, KEYEVENTF_SCANCODE, 0, ctypes.pointer(extra))
x = Input(ctypes.c_ulong(1), ii_)
# SendInput returns the number of event successfully inserted into input stream
# https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-sendinput#return-value
insertedEvents += SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))
hexKeyCode = KEYBOARD_MAPPING[key]
extra = ctypes.c_ulong(0)
ii_ = Input_I()
ii_.ki = KeyBdInput(0, hexKeyCode, keybdFlags, 0, ctypes.pointer(extra))
x = Input(ctypes.c_ulong(1), ii_)
insertedEvents += SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))
return insertedEvents == expectedEvents
# Ignored parameters: logScreenshot
# Missing feature: auto shift for special characters (ie. '!', '@', '#'...)
@_genericPyDirectInputChecks
def keyUp(key, logScreenshot=None, _pause=True):
if not key in KEYBOARD_MAPPING or KEYBOARD_MAPPING[key] is None:
return
keybdFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP
# Init event tracking
insertedEvents = 0
expectedEvents = 1
# arrow keys need the extended key flag
if key in ['up', 'left', 'down', 'right']:
keybdFlags |= KEYEVENTF_EXTENDEDKEY
hexKeyCode = KEYBOARD_MAPPING[key]
extra = ctypes.c_ulong(0)
ii_ = Input_I()
ii_.ki = KeyBdInput(0, hexKeyCode, keybdFlags, 0, ctypes.pointer(extra))
x = Input(ctypes.c_ulong(1), ii_)
# SendInput returns the number of event successfully inserted into input stream
# https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-sendinput#return-value
insertedEvents += SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))
# if numlock is on and an arrow key is being pressed, we need to send an additional scancode
# https://stackoverflow.com/questions/14026496/sendinput-sends-num8-when-i-want-to-send-vk-up-how-come
# https://handmade.network/wiki/2823-keyboard_inputs_-_scancodes,_raw_input,_text_input,_key_names
if key in ['up', 'left', 'down', 'right'] and ctypes.windll.user32.GetKeyState(0x90):
# We need to press two keys, so we expect to have inserted 2 events when done
expectedEvents = 2
hexKeyCode = 0xE0
extra = ctypes.c_ulong(0)
ii_ = Input_I()
ii_.ki = KeyBdInput(0, hexKeyCode, KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP, 0, ctypes.pointer(extra))
x = Input(ctypes.c_ulong(1), ii_)
insertedEvents += SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))
return insertedEvents == expectedEvents
# Ignored parameters: logScreenshot
# nearly identical to PyAutoGUI's implementation
@_genericPyDirectInputChecks
def press(keys, presses=1, interval=0.0, logScreenshot=None, _pause=True):
if type(keys) == str:
if len(keys) > 1:
keys = keys.lower()
keys = [keys] # If keys is 'enter', convert it to ['enter'].
else:
lowerKeys = []
for s in keys:
if len(s) > 1:
lowerKeys.append(s.lower())
else:
lowerKeys.append(s)
keys = lowerKeys
interval = float(interval)
# We need to press x keys y times, which comes out to x*y presses in total
expectedPresses = presses * len(keys)
completedPresses = 0
for i in range(presses):
for k in keys:
failSafeCheck()
downed = keyDown(k)
upped = keyUp(k)
# Count key press as complete if key was "downed" and "upped" successfully
if downed and upped:
completedPresses += 1
time.sleep(interval)
return completedPresses == expectedPresses
# Ignored parameters: logScreenshot
# nearly identical to PyAutoGUI's implementation
@_genericPyDirectInputChecks
def typewrite(message, interval=0.0, logScreenshot=None, _pause=True):
interval = float(interval)
for c in message:
if len(c) > 1:
c = c.lower()
press(c, _pause=False)
time.sleep(interval)
failSafeCheck()
write = typewrite
# Missing feature: hotkey functions