Skip to content

Instantly share code, notes, and snippets.

@tanishqmanuja
Created September 4, 2025 08:11
Show Gist options
  • Save tanishqmanuja/60db4d008051fb6ba28362d348450a5c to your computer and use it in GitHub Desktop.
Save tanishqmanuja/60db4d008051fb6ba28362d348450a5c to your computer and use it in GitHub Desktop.

USB Keypad Fixes: NumLock Sandwich + Idle First-Key Drop

1️⃣ Keebie Patch: processKeycode

def processKeycode(self, keycode):
    """Parse a command in our current layer bound to the passed keycode (ledger history)."""
    # Normalize NumLock "sandwiches" → strip KEY_NUMLOCK if other keys are present
    parts = keycode.split("-")
    filtered = [p for p in parts if p != "KEY_NUMLOCK"]
    if filtered:  # only strip if something else remains
        keycode = "-".join(filtered)
        dprint(f"{self.name} filtered {keycode}")  # debug info

    dprint(f"{self.name} is processing {keycode} in layer {self.currentLayer}")  # debug info

    if keycode in readJson(self.currentLayer):
        value = readJson(self.currentLayer)[keycode]
        value = parseVars(value, self.currentLayer)
        # ... rest of Keebie processing

2️⃣ Generic Udev Rule: /etc/udev/rules.d/92-usb-hid-no-powersave.rules

# Disable USB autosuspend for all HID devices and hubs
ACTION=="add", SUBSYSTEM=="usb", ATTR{bInterfaceClass}=="03", ATTR{power/control}="on"
ACTION=="add", SUBSYSTEM=="usb", ATTR{bDeviceClass}=="09", ATTR{power/control}="on"

Reload rules:

sudo udevadm control --reload-rules
sudo udevadm trigger

3️⃣ Keepalive Script: /usr/local/bin/keepalive.sh

#!/bin/bash
while true; do
    # Keep all USB devices authorized to prevent idle drop
    for dev in /sys/bus/usb/devices/*/authorized; do
        echo 1 > "$dev"
    done
    sleep 60
done

Make it executable:

sudo chmod +x /usr/local/bin/keepalive.sh

4️⃣ Systemd Service: /etc/systemd/system/numpad-keepalive.service

[Unit]
Description=Keep numeric keypads awake
After=multi-user.target

[Service]
Type=simple
ExecStart=/usr/local/bin/keepalive.sh
Restart=always
User=root

[Install]
WantedBy=multi-user.target

Enable and start:

sudo systemctl daemon-reload
sudo systemctl enable --now numpad-keepalive.service
sudo systemctl status numpad-keepalive.service

5️⃣ Verification

  1. Identify your keypad device:
ls /dev/input/by-id/
  1. Monitor low-level events:
sudo evtest /dev/input/eventX
  1. Press keys after idle — KP_ENTER should register immediately.
  2. Press sequences like KEY_NUMLOCK-KEY_KPENTER-KEY_NUMLOCK — Keebie should normalize to KEY_KPENTER.

Result:

  • NumLock sandwiches handled
  • First key after idle works reliably
  • Works for all HID devices, not just a specific vendor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment