Skip to content

Instantly share code, notes, and snippets.

@anddam
Last active August 5, 2023 11:13
Show Gist options
  • Save anddam/02b35522168d8010c1c08574dbfbefde to your computer and use it in GitHub Desktop.
Save anddam/02b35522168d8010c1c08574dbfbefde to your computer and use it in GitHub Desktop.
# /etc/udev/rules.d/80-keychron.rules
SUBSYSTEMS=="input", ATTRS{name}=="Keychron K2", RUN+="/usr/bin/touch /tmp/FOUNDKEYBOARD; /bin/echo 2 | /usr/bin/tee /sys/module/hid_apple/parameters/fnmode"
[ 6182.981545] usb 1-6.2: USB disconnect, device number 16
[ 6192.677898] usb 1-6.2: new full-speed USB device number 28 using xhci_hcd
[ 6192.780651] usb 1-6.2: New USB device found, idVendor=05ac, idProduct=024f
[ 6192.780660] usb 1-6.2: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[ 6192.780665] usb 1-6.2: Product: USB DEVICE
[ 6192.780670] usb 1-6.2: Manufacturer: USB Keychron K2
[ 6192.783537] input: USB Keychron K2 USB DEVICE as /devices/pci0000:00/0000:00:14.0/usb1/1-6/1-6.2/1-6.2:1.0/0003:05AC:024F.0005/input/input21
[ 6192.843147] apple 0003:05AC:024F.0005: input,hidraw2: USB HID v1.11 Keyboard [USB Keychron K2 USB DEVICE] on usb-0000:00:14.0-6.2/input0
[ 6192.844870] input: USB Keychron K2 USB DEVICE as /devices/pci0000:00/0000:00:14.0/usb1/1-6/1-6.2/1-6.2:1.1/0003:05AC:024F.0006/input/input22
[ 6192.902593] apple 0003:05AC:024F.0006: input,hidraw3: USB HID v1.11 Keyboard [USB Keychron K2 USB DEVICE] on usb-0000:00:14.0-6.2/input1
@webknjaz
Copy link

webknjaz commented Oct 7, 2020

I've found out that Keychron K8 also needs this. But enumerating every possible K* sounds unreasonable. My udevadm outputs:

$ udevadm info -a -n /dev/usb/hiddev1
[...]
  looking at parent device '/devices/pci0000:00/0000:00:14.0/usb1/1-5':
    KERNELS=="1-5"
    SUBSYSTEMS=="usb"
    DRIVERS=="usb"
    ATTRS{avoid_reset_quirk}=="0"
    ATTRS{bDeviceProtocol}=="00"
    ATTRS{quirks}=="0x0"
    ATTRS{speed}=="12"
    ATTRS{bNumConfigurations}=="1"
    ATTRS{maxchild}=="0"
    ATTRS{bDeviceClass}=="00"
    ATTRS{authorized}=="1"
    ATTRS{bNumInterfaces}==" 2"
    ATTRS{tx_lanes}=="1"
    ATTRS{bMaxPower}=="100mA"
    ATTRS{idVendor}=="05ac"
    ATTRS{devnum}=="98"
    ATTRS{idProduct}=="024f"
    ATTRS{version}==" 2.00"
    ATTRS{busnum}=="1"
    ATTRS{rx_lanes}=="1"
    ATTRS{bConfigurationValue}=="1"
    ATTRS{configuration}==""
    ATTRS{ltm_capable}=="no"
    ATTRS{devpath}=="5"
    ATTRS{bcdDevice}=="0106"
    ATTRS{removable}=="removable"
    ATTRS{bDeviceSubClass}=="00"
    ATTRS{bMaxPacketSize0}=="64"
    ATTRS{urbnum}=="56427"
    ATTRS{manufacturer}=="Keychron K8"
    ATTRS{product}=="Keychron K8"
    ATTRS{bmAttributes}=="a0"

And it seems like idVendor and idProduct are the same in K2 and K8. This hints me that it should be possible to make the rule universal matching any Keychron keyboard by replacing

-SUBSYSTEMS=="input", ATTRS{name}=="Keychron K2", RUN+="/usr/bin/touch /tmp/FOUNDKEYBOARD; /bin/echo 2 | /usr/bin/tee /sys/module/hid_apple/parameters/fnmode"
+SUBSYSTEMS=="input", ATTRS{idVendor}=="05ac", ATTRS{idProduct}=="024f", RUN+="/usr/bin/touch /tmp/FOUNDKEYBOARD; /bin/echo 2 | /usr/bin/tee /sys/module/hid_apple/parameters/fnmode"  # Any Keychron keyboard

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment