Files
nixos-config/tv-power-listener.py

57 lines
1.7 KiB
Python
Executable File

#!/usr/bin/env python3
"""
TV Power Key Listener
Listens for F14/F15 on the keyd virtual keyboard.
F14 is triggered by Caps+grave (power toggle).
F15 is triggered by Caps+1 (power off).
"""
import evdev
import subprocess
import time
KEY_ACTIONS = {
"KEY_F14": "/opt/roku-remote/toggle-power.sh",
"KEY_F15": "/opt/roku-remote/power-off.sh",
}
def find_keyd_keyboard():
"""Find the keyd virtual keyboard device."""
devices = [evdev.InputDevice(fn) for fn in evdev.list_devices()]
for d in devices:
if "keyd" in d.name.lower() and "keyboard" in d.name.lower():
return d.path
return None
def main():
while True:
try:
# Find the keyd virtual keyboard
device_path = find_keyd_keyboard()
if device_path is None:
print("Waiting for keyd virtual keyboard...", flush=True)
time.sleep(1)
continue
device = evdev.InputDevice(device_path)
print(f"Listening on {device.name} at {device_path}", flush=True)
for event in device.read_loop():
if event.type == evdev.ecodes.EV_KEY:
key_event = evdev.categorize(event)
if key_event.keystate != 1:
continue
script_path = KEY_ACTIONS.get(key_event.keycode)
if script_path:
subprocess.run([script_path], check=False)
except OSError as e:
print(f"Device error: {e}, retrying...", flush=True)
time.sleep(1)
except Exception as e:
print(f"Error: {e}, retrying...", flush=True)
time.sleep(1)
if __name__ == "__main__":
main()