69 lines
2.5 KiB
Python
69 lines
2.5 KiB
Python
#!/usr/bin/env python3
|
|
"""Tastendruck an das Tischfenster senden - ohne den Fokus zu klauen.
|
|
|
|
python3 press.py --trust # ist die Bedienungshilfen-Freigabe erteilt?
|
|
python3 press.py f # Taste 'f' an CoinPoker Game senden
|
|
python3 press.py --global f # stattdessen an das vorderste Fenster
|
|
"""
|
|
import os as _os, sys as _sys
|
|
_w = _os.path.dirname(_os.path.abspath(__file__))
|
|
while _w != "/" and not _os.path.isdir(_os.path.join(_w, "hand")):
|
|
_w = _os.path.dirname(_w)
|
|
WURZEL = _w
|
|
for _u in ("lesen", "entscheiden", "hand", "lobby", "korpus", "betrieb"):
|
|
_q = _os.path.join(_w, _u)
|
|
if _os.path.isdir(_q) and _q not in _sys.path:
|
|
_sys.path.insert(0, _q)
|
|
import ctypes
|
|
import subprocess
|
|
import sys
|
|
|
|
FW = "/System/Library/Frameworks/ApplicationServices.framework/ApplicationServices"
|
|
AS = ctypes.cdll.LoadLibrary(FW)
|
|
|
|
AS.AXIsProcessTrusted.restype = ctypes.c_bool
|
|
AS.CGEventCreateKeyboardEvent.restype = ctypes.c_void_p
|
|
AS.CGEventCreateKeyboardEvent.argtypes = [ctypes.c_void_p, ctypes.c_uint16, ctypes.c_bool]
|
|
AS.CGEventPostToPid.argtypes = [ctypes.c_int, ctypes.c_void_p]
|
|
AS.CGEventPost.argtypes = [ctypes.c_uint32, ctypes.c_void_p]
|
|
|
|
# macOS Virtuelle Tastencodes
|
|
KEYS = {
|
|
"a": 0, "s": 1, "d": 2, "f": 3, "h": 4, "g": 5, "z": 6, "x": 7, "c": 8, "v": 9,
|
|
"b": 11, "q": 12, "w": 13, "e": 14, "r": 15, "y": 16, "t": 17, "o": 31, "u": 32,
|
|
"i": 34, "p": 35, "l": 37, "j": 38, "k": 40, "n": 45, "m": 46,
|
|
"1": 18, "2": 19, "3": 20, "4": 21, "5": 23, "6": 22, "7": 26, "8": 28, "9": 25,
|
|
"0": 29, "space": 49, "enter": 36, "esc": 53, "tab": 48,
|
|
}
|
|
|
|
while True:
|
|
out = subprocess.run(["pgrep", "-f", "CoinPoker Game"], capture_output=True, text=True)
|
|
pids = [int(x) for x in out.stdout.split()]
|
|
if not pids:
|
|
print("kein Tischfenster gefunden (CoinPoker Game laeuft nicht)")
|
|
sys.exit(1)
|
|
break
|
|
|
|
args = sys.argv[1:]
|
|
if not args or args[0] == "--trust":
|
|
print("Bedienungshilfen-Freigabe erteilt:", AS.AXIsProcessTrusted())
|
|
print("Tisch-Prozesse:", pids)
|
|
sys.exit(0)
|
|
|
|
global_mode = args[0] == "--global"
|
|
key = args[-1]
|
|
code = KEYS.get(key.lower())
|
|
if code is None:
|
|
print(f"unbekannte Taste {key!r}; bekannt: {sorted(KEYS)}")
|
|
sys.exit(1)
|
|
|
|
for down in (True, False):
|
|
event = AS.CGEventCreateKeyboardEvent(None, code, down)
|
|
if global_mode:
|
|
AS.CGEventPost(0, event)
|
|
else:
|
|
for pid in pids:
|
|
AS.CGEventPostToPid(pid, event)
|
|
print(f"Taste {key!r} (code {code}) gesendet an {pids}"
|
|
f"{' (global)' if global_mode else ' (an Prozess)'}")
|