forked from KenYu910645/MapleStoryAutoLevelUp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyBoardController.py
More file actions
286 lines (257 loc) · 9.75 KB
/
KeyBoardController.py
File metadata and controls
286 lines (257 loc) · 9.75 KB
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
'''
KeyBoardController
'''
# Standard Import
import threading
import time
# Library import
import pyautogui
from pynput import keyboard
# Local import
from logger import logger
from util import is_mac
if is_mac():
import Quartz
else:
import pygetwindow as gw
pyautogui.PAUSE = 0 # remove delay
class KeyBoardController():
'''
KeyBoardController
'''
def __init__(self, cfg, args):
self.cfg = cfg
self.cmd_action = ""
self.cmd_up_down = ""
self.cmd_left_right = ""
self.cmd_up_down_last = ""
self.cmd_left_right_last = ""
self.window_title = cfg["game_window"]["title"]
self.fps = 0 # Frame per seconds
# Timer
self.t_last_up = 0.0
self.t_last_down = 0.0
self.t_last_toggle = 0.0
self.t_last_screenshot = 0.0
self.t_last_jump_down = 0.0
self.t_last_run = time.time()
self.t_last_skill = 0.0 # Last time character perform action(attack, cast spell, ...)
self.t_last_buff_cast = [0] * len(self.cfg["buff_skill"]["keys"]) # Last time cast buff skill
# Flags
self.is_enable = True
self.is_need_screen_shot = False
self.is_need_toggle = False
self.is_need_force_heal = False
self.is_terminated = False
# Parameters
self.debounce_interval = self.cfg["system"]["key_debounce_interval"]
self.fps_limit = self.cfg["system"]["fps_limit_keyboard_controller"]
# use 'ctrl', 'alt' for mac, because it's hard to get around
# macOS's security settings
if is_mac():
self.toggle_key = keyboard.Key.ctrl
self.screenshot_key = keyboard.Key.alt
self.terminate_key = keyboard.Key.esc
else:
self.toggle_key = keyboard.Key.f1
self.screenshot_key = keyboard.Key.f2
self.terminate_key = keyboard.Key.f12
# set up attack key
self.attack_key = ""
if args.attack == "aoe_skill":
self.attack_key = cfg["key"]["aoe_skill"]
elif args.attack == "directional":
self.attack_key = cfg["key"]["directional_attack"]
else:
logger.error(f"Unexpected attack argument: {args.attack}")
# Start keyboard control thread
threading.Thread(target=self.run, daemon=True).start()
listener = keyboard.Listener(on_press=self.on_press)
listener.start()
def on_press(self, key):
'''
Handle key press events.
'''
try:
# Handle regular character keys
key.char
except AttributeError:
# Handle special keys
if key == self.toggle_key:
if time.time() - self.t_last_toggle > self.debounce_interval:
self.toggle_enable()
self.t_last_toggle = time.time()
elif key == self.screenshot_key:
if time.time() - self.t_last_screenshot > self.debounce_interval:
self.is_need_screen_shot = True
self.t_last_screenshot = time.time()
elif key == self.terminate_key:
self.is_terminated = True
logger.info(f"[on_press] User press terminate key")
def toggle_enable(self):
'''
toggle_enable
'''
self.is_enable = not self.is_enable
logger.info(f"Player pressed F1, is_enable:{self.is_enable}")
# Make sure all key are released
self.release_all_key()
def press_key(self, key, duration=0.05):
'''
Simulates a key press for a specified duration
'''
if key:
pyautogui.keyDown(key)
time.sleep(duration)
pyautogui.keyUp(key)
def disable(self):
'''
disable keyboard controlller
'''
self.is_enable = False
def enable(self):
'''
enable keyboard controlller
'''
self.is_enable = True
def set_command(self, new_command):
'''
Set keyboard command
'''
self.cmd_left_right, self.cmd_up_down, self.cmd_action = new_command.split()
def is_game_window_active(self):
'''
Check if the game window is currently the active (foreground) window.
Returns:
- True
- False
'''
if is_mac():
active_window = Quartz.CGWindowListCopyWindowInfo(
Quartz.kCGWindowListOptionOnScreenOnly | Quartz.kCGWindowListExcludeDesktopElements,
Quartz.kCGNullWindowID
)
for window in active_window:
window_name = window.get(Quartz.kCGWindowName, '')
if window_name and self.window_title in window_name:
return True
return False
else:
try:
active_window = gw.getActiveWindow()
if not active_window:
return False
return self.window_title in active_window.title
except Exception as e:
return False
def release_all_key(self):
'''
Release all key
'''
pyautogui.keyUp("left")
pyautogui.keyUp("right")
pyautogui.keyUp("up")
pyautogui.keyUp("down")
# Also release attack keys to stop any ongoing attacks
pyautogui.keyUp(self.attack_key)
def limit_fps(self):
'''
Limit FPS
'''
# If the loop finished early, sleep to maintain target FPS
target_duration = 1.0 / self.fps_limit # seconds per frame
frame_duration = time.time() - self.t_last_run
if frame_duration < target_duration:
time.sleep(target_duration - frame_duration)
# Update FPS
self.fps = round(1.0 / (time.time() - self.t_last_run))
self.t_last_run = time.time()
# logger.info(f"FPS = {self.fps}")
def run(self):
'''
run
'''
while not self.is_terminated:
# Check if game window is active
if not self.is_enable or not self.is_game_window_active():
self.limit_fps()
continue
# Buff skill
for i, buff_skill_key in enumerate(self.cfg["buff_skill"]["keys"]):
cooldown = self.cfg["buff_skill"]["cooldown"][i]
if time.time() - self.t_last_buff_cast[i] >= cooldown and \
time.time() - self.t_last_skill > self.cfg["buff_skill"]["action_cooldown"]:
self.press_key(buff_skill_key)
logger.info(f"[Buff] Press buff skill key: '{buff_skill_key}' (cooldown: {cooldown}s)")
# Reset timers
self.t_last_buff_cast[i] = time.time()
self.t_last_skill = time.time()
break
# Force Heal
if self.is_need_force_heal:
self.cmd_action = "add_hp"
##########################
### Left-Right Command ###
##########################
if self.cmd_left_right == "left":
pyautogui.keyUp("right")
pyautogui.keyDown("left")
elif self.cmd_left_right == "right":
pyautogui.keyUp("left")
pyautogui.keyDown("right")
elif self.cmd_left_right == "stop":
pyautogui.keyUp("left")
pyautogui.keyUp("right")
elif self.cmd_left_right == "none":
if self.cmd_left_right_last != "none":
pyautogui.keyUp("left")
pyautogui.keyUp("right")
else:
logger.error("[KeyBoardController] Unsupported left-right command: "
f"{self.cmd_left_right}")
self.cmd_left_right_last = self.cmd_left_right
#######################
### Up-Down Command ###
#######################
if self.cmd_up_down == "up":
pyautogui.keyUp("down")
pyautogui.keyDown("up")
elif self.cmd_up_down == "down":
pyautogui.keyUp("up")
pyautogui.keyDown("down")
elif self.cmd_up_down == "stop":
pyautogui.keyUp("up")
pyautogui.keyUp("down")
elif self.cmd_up_down == "none":
if self.cmd_up_down_last != "none":
pyautogui.keyUp("up")
pyautogui.keyUp("down")
else:
logger.error("[KeyBoardController] Unsupported up-down command: "
f"{self.cmd_up_down}")
self.cmd_up_down_last = self.cmd_up_down
######################
### Action Command ###
######################
if self.cmd_action == "jump":
self.press_key(self.cfg["key"]["jump"])
elif self.cmd_action == "teleport":
self.press_key(self.cfg["key"]["teleport"])
elif self.cmd_action == "attack":
self.press_key(self.attack_key)
self.t_last_skill = time.time()
elif self.cmd_action == "add_hp":
self.press_key(self.cfg["key"]["add_hp"])
self.cmd_action = "none" # Reset command
elif self.cmd_action == "add_mp":
self.press_key(self.cfg["key"]["add_mp"])
self.cmd_action = "none" # Reset command
elif self.cmd_action == "goal":
pass
elif self.cmd_action == "none":
pass
else:
logger.error("[KeyBoardController] Unsupported action command: "
f"{self.cmd_action}")
self.limit_fps()
self.release_all_key() # Prevent key keep press down after termination