-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_mousemovement.py
81 lines (71 loc) · 2.13 KB
/
test_mousemovement.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
# should always be 1 try per movement.
# Otherwise there may be an issue with mouse acceleration
# Which will cause movements that happen within 0.3 secs of each other
# to be greater than intended.
import uinput
import time
import random
from PyQt4.QtGui import QPixmap, QApplication, QImage, QCursor
app = QApplication([])
device = uinput.Device((uinput.REL_X, uinput.REL_Y, uinput.BTN_LEFT))
# pos = QCursor.pos()
# x,y = pos.x(), pos.y()
# print x
# print y
time.sleep(2)
# dest = {"x": 1500, "y": 200}
# device.emit(uinput.REL_X, dest[0]-x, syn = False)
# device.emit(uinput.REL_Y, dest[1]-y)
# print QCursor.pos()
def move_or_print(target, tries=0):
pos = QCursor.pos()
device.emit(uinput.REL_X, target['x']-pos.x(), syn = False)
device.emit(uinput.REL_Y, target['y']-pos.y())
newpos = QCursor.pos()
if newpos.x() != target['x'] or newpos.y() != target['y']:
if tries<10:
return 1+move_or_print(target, tries+1)
else:
if newpos.x() == 0:
time.sleep(1)
device.emit(uinput.REL_X, target['x']-newpos.x(), syn = False)
device.emit(uinput.REL_Y, target['y']-newpos.y())
newpos2 = QCursor.pos()
print "maximum depth exceeded"
print 'x:', target['x'], 'y:', target['y']
print pos, newpos, newpos2
return 0
def move_or_print_static(target):
tries = 0
max_tries = 8
oldpos = []
pos = QCursor.pos()
while tries < max_tries and (pos.x(), pos.y()) != (target['x'], target['y']):
# if (pos.x(), pos.y()) == (0,0):
# tries += 0.1
device.emit(uinput.REL_X, target['x']-pos.x(), syn = False)
device.emit(uinput.REL_Y, target['y']-pos.y())
tries += 1
oldpos.append(pos)
pos = QCursor.pos()
# if tries == 2:
# print "maximum depth exceeded"
# print 'x:', target['x'], 'y:', target['y']
# print oldpos
# print pos
return tries
dests = []
for i in range(100):
dests.append({"x": int(random.random()*900+100), "y": int(random.random()*900+100)})
a = time.time()
s = 0
reslist = []
for i in range(100):
res = move_or_print_static(dests[i])
# time.sleep(0.275)
s += res
reslist.append(res)
print time.time() - a
print s/100.0, "tries per iteration"
print QCursor.pos(), dests[-1]['x'], dests[-1]['y']
print reslist