key_extendr clened up and readme updated
This commit is contained in:
@@ -33,6 +33,7 @@ exec:
|
||||
|
||||
stop:
|
||||
-@sshpass -p "$(PASSWD)" ssh "$(TARGET)" 'for i in {0..25}; do P4wnP1_cli hid job cancel $$i; done' > /dev/null 2>&1 || true
|
||||
-@pkill key_extendr
|
||||
@echo "[i] Stopped all running jobs"
|
||||
|
||||
## Sync and execute
|
||||
|
||||
+41
-34
@@ -3,16 +3,11 @@ import evdev
|
||||
import uinput
|
||||
import time
|
||||
import threading
|
||||
import os, signal
|
||||
import os, signal, sys
|
||||
|
||||
HOLD_DURATION = 0.1 # 100ms
|
||||
DEV_NAME_FRAG = "just an usb stick"
|
||||
|
||||
|
||||
# root only needed without input group and udev rules!!
|
||||
#if os.geteuid() != 0:
|
||||
# raise SystemExit("Error: run as root (sudo)")
|
||||
|
||||
def find_device(name_fragment, exclude="Mouse"):
|
||||
for path in evdev.list_devices():
|
||||
dev = evdev.InputDevice(path)
|
||||
@@ -24,43 +19,55 @@ def find_device(name_fragment, exclude="Mouse"):
|
||||
raise RuntimeError(f"No device matching '{name_fragment}' found")
|
||||
|
||||
def hold_key(key, dev):
|
||||
dev.emit((evdev.ecodes.EV_KEY, key), 1) # key down
|
||||
time.sleep(HOLD_DURATION)
|
||||
dev.emit((evdev.ecodes.EV_KEY, key), 0) # key up
|
||||
try:
|
||||
dev.emit((evdev.ecodes.EV_KEY, key), 1)
|
||||
time.sleep(HOLD_DURATION)
|
||||
dev.emit((evdev.ecodes.EV_KEY, key), 0)
|
||||
except OSError as e:
|
||||
print(f"[ERROR] emit failed for key {key}: {e}", file=sys.stderr)
|
||||
|
||||
def handle_exit(sig, frame):
|
||||
print("\nExiting...")
|
||||
raise SystemExit(0)
|
||||
|
||||
try:
|
||||
keyboard = uinput.Device([
|
||||
uinput.KEY_UP,
|
||||
uinput.KEY_DOWN,
|
||||
uinput.KEY_LEFT,
|
||||
uinput.KEY_RIGHT,
|
||||
uinput.KEY_ENTER,
|
||||
uinput.KEY_X,
|
||||
uinput.KEY_Z,
|
||||
uinput.KEY_A,
|
||||
uinput.KEY_S,
|
||||
uinput.KEY_TAB,
|
||||
uinput.KEY_BACKSPACE,
|
||||
])
|
||||
except OSError as e:
|
||||
print(f"[ERROR] Failed to open uinput device: {e}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
keyboard = uinput.Device([
|
||||
uinput.KEY_UP,
|
||||
uinput.KEY_DOWN,
|
||||
uinput.KEY_LEFT,
|
||||
uinput.KEY_RIGHT,
|
||||
uinput.KEY_ENTER,
|
||||
uinput.KEY_X,
|
||||
uinput.KEY_Z,
|
||||
uinput.KEY_A,
|
||||
uinput.KEY_S,
|
||||
uinput.KEY_TAB,
|
||||
uinput.KEY_BACKSPACE,
|
||||
])
|
||||
source = find_device(DEV_NAME_FRAG)
|
||||
try:
|
||||
source = find_device(DEV_NAME_FRAG)
|
||||
except RuntimeError as e:
|
||||
print(f"[ERROR] {e}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
signal.signal(signal.SIGINT, handle_exit)
|
||||
signal.signal(signal.SIGTERM, handle_exit)
|
||||
|
||||
print(f"Listening on '{source.name}' ...")
|
||||
|
||||
for event in source.read_loop():
|
||||
if event.type == evdev.ecodes.EV_KEY:
|
||||
if event.value == 1: # key down only
|
||||
threading.Thread(
|
||||
target=hold_key,
|
||||
args=(
|
||||
event.code,
|
||||
keyboard
|
||||
),
|
||||
daemon=True
|
||||
).start()
|
||||
try:
|
||||
for event in source.read_loop():
|
||||
if event.type == evdev.ecodes.EV_KEY:
|
||||
if event.value == 1:
|
||||
threading.Thread(
|
||||
target=hold_key,
|
||||
args=(event.code, keyboard),
|
||||
daemon=True
|
||||
).start()
|
||||
except OSError as e:
|
||||
print(f"[ERROR] Device read error (disconnected?): {e}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
Reference in New Issue
Block a user