-
Notifications
You must be signed in to change notification settings - Fork 7
/
MagicKeys.lua
264 lines (221 loc) · 6.47 KB
/
MagicKeys.lua
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
--[[
MagicKeys and MKLib Lua wrapper
The wrapper implements all methods and objects obey the Lua codestyle.
]]--
-- coordinates and size conversion
-- All methods which wait x, y, w or h need converted
position = {}
-- getting the relative coordinates
position.module = setmetatable({}, {
-- The table indexing must be referenced to MagicKeys coordinates object
__index = MagicKeys.Coords,
-- When the object called as function it mus convert passed values to expected table
__call = function (self, x, y, w, h)
return {
x = MagicKeys.Coords.X + (x or 0),
y = MagicKeys.Coords.Y + (y or 0),
-- The width and height fields are optional, so we don't need to check these
w = w,
h = h
}
end
})
-- Getting an absolute coordinates
-- Basically, it is just plug for easy substitution
position.screen = setmetatable({}, {
__index = function ()
return 0
end,
__call = function (self, x, y, w, h)
return {
x = x or 0, y = y or 0,
w = w, h = h
}
end
})
-- Clipboard manipulations
clipboard = {}
function clipboard.getText()
return MKLib.GetText()
end
-- Show debug form
---@Param debugInfo (string)
function debugForm(debugInfo)
MKLib.CallDebugForm(debugInfo)
end
-- User input implementations
inputBox ={}
-- Allows to get the numeric value using spinbox
---@param title (string): the window title
---@param caption (string): the instruction for typing
---@param min (number): the minimal spinbox value
---@param max (number): the maximal spinbox value
---@return 1 (boolean): true when OK button is presset, false otherwise
---@return 2 (number): specified numeric value
function inputBox.spinner(title, caption, min, max)
local retval = MKLib.InputBox(title, caption, min, max)
return retval ~= nil, tonumber(retval or 0)
end
function inputBox.string(title, caption, multiline)
local retval = MKLib.InputTextBox(title, caption, multiline or false)
return retval ~= nil, tostring(retval or "")
end
keyboard = {}
-- Implement the send type proxy
setmetatable(keyboard, {
__index = function (self, key)
if key:match("^send") then
local type = key:match("^send(.+)")
return function (keys, time, postTime)
time = time or self.time or 50
postTime = postTime or self.postTime or 100
MKLib.KeySend(type, keys, time, postTime)
end
end
end
})
screen = {}
function screen.recognizeText(position, Zoom, Lang)
return MKLib.ImgToText(position.w, position.h, position.x, position.y, Zoom, Lang or "en")
end
function screen.isPixelColor(coords, Color)
return MKLib.PixelSearch(coords.x, coords.y, Color)
end
function screen.searchImage(imgPath, window)
local retval = MagicKeys.ImageSearch(imgPath, window)
local pos = nil
if retval[0] == 1 then
pos = {
x = retval[1],
y = retval[2],
w = retval[3],
h = retval[4],
cx = retval[5],
cy = retval[6]
}
end
return retval[1] == 1, pos
end
function screen.searchImageAt(imgPath, position, variant)
local retval = MagicKeys.ImageSearchArea(imgPath, position.x, position.y, position.w, position.h, variant)
local pos = nil
if retval[0] == 1 then
pos = {
x = retval[1],
y = retval[2],
w = retval[3],
h = retval[4],
cx = retval[5],
cy = retval[6]
}
end
return retval[1] == 1, pos
end
function screen.snapshot(position)
MKLib.CreateBitmap(position.x, position.y, position.w, position.h)
end
function screen.waitUntilChanged(position, SkipPixelX, SkipPixelY, CountChangedPixel)
MKLib.DisplayTraffic(position.x, position.y, position.w, position.h, SkipPixelX, SkipPixelY, CountChangedPixel)
end
mouse = {}
function mouse.move(coords, timeMove)
MKLib.MouseMove(coords.x, coords.y, timeMove or mouse.timeMove or 0)
end
setmetatable(mouse, {
__index = function (self, key)
if key:match("^click") then
local button = key:match("^click(.+)")
return function (coords, Count, TimeMove, TimeClick, TimeUp)
--Default definitions
Count = Count or 1
-- The following properties might initialized in the mouse for all module work time
TimeMove = TimeMove or self.timeMove or 0
TimeClick = TimeClick or self.timeClick or 0
TimeUp = TimeUp or self.timeUp or 1
-- Getting decide where we need to click
local x, y = coords.x, coords.y
if coords.w and coords.h then
x = math.modf((x + coords.w) * 0.33)
y = math.modf((y + coords.h) * 0.33)
end
MKLib.MouseClick(button, x, y, Count, TimeMove, TimeClick, TimeUp)
end
elseif key:match("^drag") then
local button = key:match("^drag(.+)")
return function (coordsFrom, coordsTo, TimeMove, TimeUp)
--Default definitions
-- The following properties might initialized in the mouse for all module work time
TimeMove = TimeMove or self.timeMove or 0
TimeUp = TimeUp or self.timeUp or 1
MKLib.MouseClickDrag(button, coordsFrom.x, coordsFrom.y, coordsTo.x, coordsTo.y, TimeMove, TimeUp)
end
end
end
})
menu = {}
function menu.countItems()
return MKLib.GetMICount()
end
function menu.getItemHandle()
return MKLib.GetMIHandle()
end
sound = {}
function sound.play(soundFile, waitUntilPlayed)
MagicKeys.SoundPlay(soundFile, waitUntilPlayed or false)
end
-- VUI interraction
vui = {}
function vui.load(VUIName)
MagicKeys.VUILoader(VUIName)
end
function vui.nextObject()
MagicKeys.VUIObjectNavigator("Next")
end
function vui.previousObject()
MagicKeys.VUIObjectNavigator("Back")
end
function vui.speakCurrentObject()
MagicKeys.VUIObjectSpeak()
end
function vui.updateObjects(resetPosition)
MagicKeys .VUIObjectsUpdate(resetPosition)
end
-- Get visible VVUI objects array
function vui.getVisibleObjects()
local constructor = {}
-- C# contains the array index from 0
for i = 0, #MagicKeys.VisibleVUIObjects-1 do
local vuiObj = MagicKeys.VisibleVUIObjects[i]
local fixedvuiObj = {}
for key, value in pairs(vuiObj) do
fixedvuiObj[string.gsub(key, "^%u", string.lower)] = value
end
table.insert(constructor, fixedvuiObj)
end
return constructor
end
-- Plugin namespace
plugin = {}
function plugin.getPaths()
return {
plugin = MagicKeys.API.GetPluginPath(),
vui = MagicKeys.API.GetVUIPath(),
lua = MagicKeys.API.GetLUAPath(),
images = MagicKeys.API.GetImgPath()
}
end
-- Window and controls identification and interraction
window = {}
function window.setFocusByCoordinates(coords)
MKLib.SetFocusControlPoint(coords.x, coords.y)
end
function window.waitUntilClosed(windowClass)
MKLib.WaitWinClose(windowClass)
end
-- Root functions which might called at anytime
function sleep(Time)
MKLib.Sleep(Time)
end
function speak(Text, waitUntilSpeak)
MKLib.Speak(Text, waitUntilSpeak or false)
end