-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathremap_pc.py
More file actions
66 lines (60 loc) · 2.48 KB
/
remap_pc.py
File metadata and controls
66 lines (60 loc) · 2.48 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
"""
ooooooooo oooo oooo o ooooooooooo ooooooooooo
888 88o 888 o88 888 888 888
888 888 888888 8 88 888ooo8 888ooo8
888 888 888 88o 8oooo88 888 888
o888ooo88 o888o o888o o88o o888o o888o o888ooo8888
by Jon Wilson (10yard)
Remap Controls for PC and DOS
which can't be otherwise configured
-----------------------------------
"""
from subprocess import call, DEVNULL, STDOUT, CREATE_NO_WINDOW
import keyboard
import sys
import ctypes
import pygetwindow
def kill_pc_external(pid=None, program=None):
"""force kill an external program"""
if pid:
call(f"taskkill /f /PID {pid}", stdout=DEVNULL, stderr=STDOUT, creationflags=CREATE_NO_WINDOW)
elif program:
call(f"taskkill /f /IM {program}", stdout=DEVNULL, stderr=STDOUT, creationflags=CREATE_NO_WINDOW)
# Ensure remappings are ended
keyboard.unhook_all()
call(f"taskkill /f /IM remap_pc.exe")
def remap(name, mappings):
"""asynchronous: temporary keyboard remapping and force quit option"""
# Move mouse cursor to the bottom right corner
ctypes.windll.user32.SetCursorPos(999999, 999999)
# Remap controls and other special functions
for m in mappings.split("|"):
try:
src, dst = m.split(">")
except ValueError:
continue
if dst.startswith("forcequit"):
# Force quit necessary for some PC games. "forcequit:PROGRAMNAME" or "forcequit" to kill the default.
if ":" in dst:
_program = dst.split(":")[1]
else:
_program = f"{name}.*"
keyboard.add_hotkey(src, lambda: kill_pc_external(program=_program))
elif src.startswith("delayspace"):
keyboard.call_later(fn=keyboard.send,args=" ", delay=float(dst))
elif src.startswith("delayenter"):
keyboard.call_later(fn=keyboard.send, args="\n", delay=float(dst))
elif src.startswith("maximize"):
win = pygetwindow.getWindowsWithTitle(dst)
if win:
win[0].maximize()
else:
keyboard.remap_key(src, dst)
while True:
keyboard.wait()
if __name__ == "__main__":
if len(sys.argv) >= 3:
remap(sys.argv[1], sys.argv[2])
else:
ctypes.windll.user32.MessageBoxW(0, "This program is not intended to be run directly.",
"DKAFE Keyboard Remapping", 0)