import evdev
from evdev import InputDevice, categorize, ecodes, list_devices

print("Listing all input devices...\n")

devices = [InputDevice(path) for path in list_devices()]

if not devices:
    print("❌ No input devices found!")
    exit(1)

for device in devices:
    print(f"🔌 Found device: {device.name} at {device.path}")

print("\nListening to all devices... Press Ctrl+C to stop.\n")

# Create a dictionary of InputDevice objects
device_map = {dev.fd: dev for dev in devices}

# Use a selector to monitor all devices
from select import select

try:
    while True:
        r, _, _ = select(device_map, [], [])
        for fd in r:
            device = device_map[fd]
            for event in device.read():
                if event.type == ecodes.EV_KEY:
                    key_event = categorize(event)
                    print(f"[{device.name}] {key_event.keycode} - value: {event.value}")
except KeyboardInterrupt:
    print("\nStopped monitoring.")
