Created
February 4, 2015 09:56
-
-
Save bluetech/c5839c78278200c668e0 to your computer and use it in GitHub Desktop.
Redirecting keycodes in the evdev layer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* In linux, the evdev subsystem already has an indirection layer for | |
* keyboard keys. It allows to remap the evdev scancode it gets from | |
* the keyboard to another scancode (keycode). */ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <fcntl.h> | |
#include <unistd.h> | |
#include <linux/input.h> | |
int | |
main(void) | |
{ | |
int fd; | |
int ret; | |
unsigned scancode_keycode[2]; | |
/* Open the desired keyboard device. The paths can be different on | |
* different systems. | |
* You need root for this (but with systemd I think you can get the | |
* fd without root if you are the active user). */ | |
fd = open("/dev/input/event0", O_CLOEXEC); | |
if (fd < 0) { | |
fprintf(stderr, "couldn't open device: %s\n", strerror(-fd)); | |
goto err; | |
} | |
/* This is how you get the current keycode. Normally it's == scancode. | |
* You can see the full list of possible keycodes/scancodes in | |
* /usr/include/linux/input.h. */ | |
scancode_keycode[0] = KEY_1; | |
scancode_keycode[1] = 0; | |
ret = ioctl(fd, EVIOCGKEYCODE, scancode_keycode); | |
if (ret < 0) { | |
fprintf(stderr, "couldn't get keycode: %s\n", strerror(-ret)); | |
goto err_fd; | |
} | |
/* This is how you set a new keycode. | |
* To revert change this back to KEY_1. */ | |
scancode_keycode[0] = KEY_1; | |
scancode_keycode[1] = KEY_2; | |
ret = ioctl(fd, EVIOCSKEYCODE, scancode_keycode); | |
if (ret < 0) { | |
fprintf(stderr, "couldn't set keycode: %s\n", strerror(-ret)); | |
goto err_fd; | |
} | |
return 0; | |
err_fd: | |
close(fd); | |
err: | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment