-
Notifications
You must be signed in to change notification settings - Fork 59
/
key.py
70 lines (59 loc) · 1.96 KB
/
key.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
# For Windows
# http://stackoverflow.com/questions/1823762/sendkeys-for-python-3-1-on-windows
# https://stackoverflow.com/a/38888131
import win32api
import win32con
import win32gui
import time, sys
keyDelay = 0.1
# https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
keymap = {
"Up": win32con.VK_UP,
"Left": win32con.VK_LEFT,
"Down": win32con.VK_DOWN,
"Right": win32con.VK_RIGHT,
"b": 0x42, # ord("B"),
"a": 0x41, # ord("A"),
"y": 0x59, # ord("Y"), # for DS
"x": 0x58, # ord("X"), # for DS
"s": 0x53, # ord("S"), # Start
"e": 0x45, # ord("E"), # Select
}
# this way has to keep window in focus
def sendKey(button):
win32api.keybd_event(keymap[button], 0, 0, 0)
time.sleep(keyDelay)
win32api.keybd_event(keymap[button], 0, win32con.KEYEVENTF_KEYUP, 0)
def SimpleWindowCheck(windowname):
window = None
try:
window = win32gui.FindWindow(windowName, None)
except win32gui.error:
try:
window = win32gui.FindWindow(None, windowName)
except win32gui.error:
return False
else:
return window
else:
return window
if __name__ == "__main__":
windowName = sys.argv[1]
key = sys.argv[2]
winId = SimpleWindowCheck(windowName)
# winId = None
if not (winId):
windowList = []
def enumHandler(hwnd, list):
if windowName in win32gui.GetWindowText(hwnd):
list.append(hwnd)
win32gui.EnumWindows(enumHandler, windowList)
# only the first id, may need to try the others
winId = windowList[0]
# can check with this
for hwnd in windowList:
hwndChild = win32gui.GetWindow(hwnd, win32con.GW_CHILD)
# print("window title/id/child id: ", win32gui.GetWindowText(hwnd), "/", hwnd, "/", hwndChild)
win32gui.ShowWindow(winId, win32con.SW_SHOWNORMAL)
win32gui.SetForegroundWindow(winId)
sendKey(key)