-
Notifications
You must be signed in to change notification settings - Fork 14
/
edge.py
82 lines (62 loc) · 2.31 KB
/
edge.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
# https://supermarioemulator.com/mario.php
import sched
from time import sleep, time
import win32gui, win32ui, win32con, win32api
# http://www.kbdedit.com/manual/low_level_vk_list.html
VK_KEY_W = 0x57
VK_KEY_A = 0x41
VK_KEY_S = 0x53
VK_KEY_D = 0x44
VK_KEY_P = 0x50
VK_SHIFT = 0xA0
def main():
# init window hanle
window_name = "Super Mario Bros in HTML5 - Profile 1 - Microsoft Edge"
#hwnd = win32gui.FindWindow(None, window_name)
hwnds = find_all_windows(window_name)
print("Num windows:" + str(len(hwnds)))
sleep(1.0)
s = sched.scheduler(time, sleep)
offset_secs = 1.0
for hwnd in hwnds:
press_key(hwnd, s, VK_KEY_P, 0.1 + offset_secs, 0.1)
press_key(hwnd, s, VK_KEY_D, 0.6 + offset_secs, 1.95)
press_key(hwnd, s, VK_KEY_W, 2.5 + offset_secs, 0.9)
press_key(hwnd, s, VK_KEY_D, 3.3 + offset_secs, 1.05)
press_key(hwnd, s, VK_KEY_W, 3.5 + offset_secs, 0.8)
offset_secs += 3.31
s.run()
# send a keyboard input to the given window
def press_key(hwnd, s, key, start_sec, hold_sec):
priority = 2
duration = start_sec + hold_sec
s.enter(start_sec, priority, win32api.SendMessage,
argument=(hwnd, win32con.WM_KEYDOWN, key, 0))
s.enter(duration, priority, win32api.SendMessage,
argument=(hwnd, win32con.WM_KEYUP, key, 0))
# win32gui.SetForegroundWindow(hwnd)
# win32api.SendMessage(hwnd, win32con.WM_KEYDOWN, key, 0)
# sleep(sec)
# win32api.SendMessage(hwnd, win32con.WM_KEYUP, key, 0)
def list_window_names():
def winEnumHandler(hwnd, ctx):
if win32gui.IsWindowVisible(hwnd):
print(hex(hwnd), '"' + win32gui.GetWindowText(hwnd) + '"')
win32gui.EnumWindows(winEnumHandler, None)
def get_inner_windows(whndl):
def callback(hwnd, hwnds):
if win32gui.IsWindowVisible(hwnd) and win32gui.IsWindowEnabled(hwnd):
hwnds[win32gui.GetClassName(hwnd)] = hwnd
return True
hwnds = {}
win32gui.EnumChildWindows(whndl, callback, hwnds)
return hwnds
def find_all_windows(name):
result = []
def winEnumHandler(hwnd, ctx):
if win32gui.IsWindowVisible(hwnd) and win32gui.GetWindowText(hwnd) == name:
result.append(hwnd)
win32gui.EnumWindows(winEnumHandler, None)
return result
main()
#list_window_names()