Skip to content

Instantly share code, notes, and snippets.

@tanyuan
Created October 10, 2016 04:56
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanyuan/6233e0d3106dfc7849d959c7df61c79c to your computer and use it in GitHub Desktop.
Save tanyuan/6233e0d3106dfc7849d959c7df61c79c to your computer and use it in GitHub Desktop.
Fix Apple keyboard Caps Lock delay.

Fix Apple keyboard Caps Lock delay.

Useful when remapped to another key.

Compile the code:

    gcc apple-kb-caps.c -o apple-kb-caps

Move the executable apple-kb-caps and script run-apple-kb-caps to ~/bin/.

Run the script (require sudo password):

    run-apple-kb-caps 

Credit: jmrk

#include <linux/hidraw.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv) {
if (argc != 2 || strcmp(argv[1], "-h") == 0) {
printf("Pass a hidraw device as the first and only parameter!\n");
printf("You may find the right device with:\n");
printf(" dmesg | grep apple | grep Keyboard | grep input0 | tail -1 | "
"sed -e 's/.*hidraw\\([[:digit:]]\\+\\).*/\\/dev\\/hidraw\\1/'\n");
printf("For example: /dev/hidraw1\n");
return 1;
}
int fd, i, res, desc_size = 0;
char buf[256];
struct hidraw_devinfo info;
char *device = argv[1];
fd = open(device, O_RDWR | O_NONBLOCK);
if (fd < 0) {
perror("Unable to open device");
return 1;
}
memset(&info, 0, sizeof(info));
memset(buf, 0, sizeof(buf));
// Get Report Descriptor Size
res = ioctl(fd, HIDIOCGRDESCSIZE, &desc_size);
if (res < 0)
perror("HIDIOCGRDESCSIZE");
if (desc_size != 75) {
printf("Error: unexpected descriptor size %d; you've probably got "
"the wrong hidraw device!\n", desc_size);
return 1;
}
// Get Raw Info
res = ioctl(fd, HIDIOCGRAWINFO, &info);
if (res < 0) {
perror("HIDIOCGRAWINFO");
} else {
if (info.vendor != 0x05ac) {
printf("Error: Wrong vendor ID, make sure you got the right "
"hidraw device!\n");
return 1;
}
if (info.product != 0x0251) {
printf("Warning: Unknown product ID 0x%x!\n", info.product);
}
}
// Get Feature
buf[0] = 0x09; // Report Number
res = ioctl(fd, HIDIOCGFEATURE(256), buf);
if (res < 0) {
perror("HIDIOCGFEATURE");
} else {
printf("HID Feature Report (before change):\n\t");
for (i = 0; i < res; i++) printf("%hhx ", buf[i]);
puts("\n");
}
// Set Feature
buf[0] = 0x09; // Report Number
buf[1] = 0x00; // Report data
buf[2] = 0x00; // padding
buf[3] = 0x00; // padding
res = ioctl(fd, HIDIOCSFEATURE(4), buf);
if (res < 0)
perror("HIDIOCSFEATURE");
else
printf("Caps lock delay disabled.\n");
// Get Feature
buf[0] = 0x09; // Report Number
res = ioctl(fd, HIDIOCGFEATURE(256), buf);
if (res < 0) {
perror("HIDIOCGFEATURE");
} else {
printf("HID Feature Report (after change):\n\t");
for (i = 0; i < res; i++) printf("%hhx ", buf[i]);
puts("\n");
}
close(fd);
return 0;
}
#!/bin/bash
# Disable Apple Keyboard Caps Lock Key Delay
# apple-kb-caps /dev/hidraw1
sudo ~/bin/apple-kb-caps `dmesg | grep apple | grep Keyboard | grep input0 | tail -1 | sed -e 's/.*hidraw\([[:digit:]]\+\).*/\/dev\/hidraw\1/'`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment