This repository has been archived by the owner on Apr 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PyMacroV2.py
168 lines (138 loc) · 4.7 KB
/
PyMacroV2.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
from math import *
import pynput
from pynput.keyboard import Key, KeyCode
from pynput.mouse import Button
from imagesearch import *
import time, win32gui, win32con, win32com.client
_mouse = pynput.mouse.Controller()
_keyboard = pynput.keyboard.Controller()
ESC = Key.esc
def Key_(char):
return KeyCode(char=char)
def KeyDown(key):
_keyboard.press(key)
def KeyUp(key):
_keyboard.release(key)
def KeyPress(key):
KeyDown(key)
Delay(STATIC_DELAY)
KeyUp(key)
# print("[debug]pressed "+str(key))
def MoveMouse(dx, dy):
_mouse.position = (dx, dy)
def MouseLDown():
_mouse.press(pynput.mouse.Button.left)
def MouseLUp():
_mouse.release(pynput.mouse.Button.left)
def MouseLPress(dx, dy):
MoveMouse(dx, dy)
Delay(STATIC_DELAY)
MouseLDown()
Delay(STATIC_DELAY)
MouseLUp()
# print("[debug]clicked at ("+str(dx)+","+str(dy)+")") # debug
def MouseLDrag(dx1, dy1, dx2, dy2):
MoveMouse(dx1, dy1)
Delay(STATIC_DELAY)
MouseLDown()
Delay(STATIC_DELAY)
MoveMouse(dx2, dy2)
Delay(STATIC_DELAY)
MouseLUp()
def MouseRDown():
_mouse.press(pynput.mouse.Button.right)
def MouseRUp():
_mouse.release(pynput.mouse.Button.right)
def MouseRPress(dx, dy):
MoveMouse(dx, dy)
Delay(STATIC_DELAY)
MouseRDown()
Delay(STATIC_DELAY)
MouseRUp()
# print("[debug]clicked at ("+str(dx)+","+str(dy)+")") # debug
def MouseRDrag(dx1, dy1, dx2, dy2):
MoveMouse(dx1, dy1)
Delay(STATIC_DELAY)
MouseRDown()
Delay(STATIC_DELAY)
MoveMouse(dx2, dy2)
Delay(STATIC_DELAY)
MouseRUp()
def Delay(sec):
time.sleep(sec)
def WindowExists(title):
if win32gui.FindWindow(None, title) == 0:
return False
return True
def ShowWindowByHWND(hwnd):
# win32gui.ShowWindow(hwnd, win32con.SW_MINIMIZE)
# win32gui.ShowWindow(hwnd, win32con.SW_RESTORE)
win32gui.ShowWindow(hwnd, win32con.SW_SHOWNOACTIVATE)
win32gui.ShowWindow(hwnd, win32con.SW_SHOW)
try:
win32gui.SetForegroundWindow(hwnd)
except:
try:
win32gui.BringWindowToTop(hwnd)
except:
pass
def SwitchToWindow(title):
if WindowExists(title):
hwnd = win32gui.FindWindow(None, title)
win32gui.ShowWindow(hwnd, win32con.SW_MINIMIZE)
win32gui.ShowWindow(hwnd, win32con.SW_RESTORE)
win32com.client.Dispatch("WScript.Shell").AppActivate(title)
def MinimizeWindow(title):
if WindowExists(title):
win32gui.ShowWindow(win32gui.FindWindow(None, title), win32con.SW_MINIMIZE)
def MoveWindow(title, x, y):
if WindowExists(title):
SwitchToWindow(title)
hwnd = win32gui.FindWindow(None, title)
rect = win32gui.GetWindowRect(hwnd)
win32gui.MoveWindow(hwnd, x, y, rect[2]-rect[0], rect[3]-rect[1], True)
def FindImage(fn, x1, y1, x2, y2, precision):
# SaveImage("[debug]prtscr at ("+str(x1)+","+str(y1)+","+str(x2)+","+str(y2)+")",x1,y1,x2,y2) # debug
return imagesearcharea(fn, x1, y1, x2, y2, precision)
def SaveImage(fn, x1, y1, x2, y2):
im = region_grabber(region=(x1, y1, x2, y2))
if is_retina:
im.thumbnail((round(im.size[0] * 0.5), round(im.size[1] * 0.5)))
im.save(fn+".png")
def MoveToImage(fn, x1, y1, x2, y2, precision):
pos = FindImage(fn, x1, y1, x2, y2, precision)
if pos[0] == -1:
# print("[debug]"+fn+" not found in ("+str(x1)+","+str(y1)+","+str(x2)+","+str(y2)+")") # debug
return False
img = cv2.imread(fn)
height, width, channels = img.shape
# print("[debug]clicked at ("+str(pos[0])+","+str(pos[0])+")") # debug
MoveMouse(pos[0]+width/2, pos[1]+height/2)
return True
def ClickOnImage(fn, x1, y1, x2, y2, precision):
pos = FindImage(fn, x1, y1, x2, y2, precision)
if pos[0] == -1:
# print("[debug]"+fn+" not found in ("+str(x1)+","+str(y1)+","+str(x2)+","+str(y2)+")") # debug
return False
img = cv2.imread(fn)
height, width, channels = img.shape
# print("[debug]clicked at ("+str(pos[0])+","+str(pos[0])+")") # debug
MouseLPress(pos[0]+width/2, pos[1]+height/2)
return True
def ClickOnImageLoop(fn, x1, y1, x2, y2, precision, sec):
pos = FindImage(fn, x1, y1, x2, y2, 0.8)
while pos[0] == -1:
print("[debug]Retrying "+fn)
pos = FindImage(fn, x1, y1, x2, y2, 0.8)
Delay(sec)
img = cv2.imread(fn)
height, width, channels = img.shape
MouseLPress(pos[0]+width/2, pos[1]+height/2)
def ALT_TAB():
KeyDown(Key.alt)
KeyDown(Key.tab)
KeyUp(Key.tab)
KeyUp(Key.alt)
STATIC_DELAY = 0.1
def DictToObject(dict):
return type('',(object,),dict)()