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.

Revisions

  1. tanishqmanuja created this gist Sep 4, 2025.
    113 changes: 113 additions & 0 deletions keebie-numpad-fix.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,113 @@
    # **USB Keypad Fixes: NumLock Sandwich + Idle First-Key Drop**

    ## 1️⃣ Keebie Patch: `processKeycode`

    ```python
    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`

    ```bash
    # 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:

    ```bash
    sudo udevadm control --reload-rules
    sudo udevadm trigger
    ```

    ---

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

    ```bash
    #!/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:

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

    ---

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

    ```ini
    [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:

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

    ---

    ## 5️⃣ Verification

    1. Identify your keypad device:

    ```bash
    ls /dev/input/by-id/
    ```

    2. Monitor low-level events:

    ```bash
    sudo evtest /dev/input/eventX
    ```

    3. Press keys after idle — `KP_ENTER` should register immediately.
    4. 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