Skip to content

Instantly share code, notes, and snippets.

@Wonicon
Last active September 27, 2015 07:29
Show Gist options
  • Save Wonicon/9e16740e530d4e590ec7 to your computer and use it in GitHub Desktop.
Save Wonicon/9e16740e530d4e590ec7 to your computer and use it in GitHub Desktop.
Detect CAPSLOCK
// I find the excellent code at http://stackoverflow.com/a/20946151
// Put it here for further work on the CAPSLOCK detection prog
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <linux/input.h>
#include <string.h>
#include <stdio.h>
static const char *const evval[3] = {
"RELEASED",
"PRESSED",
"REPEATED"
};
int main() {
const char *dev = "/dev/input/by-path/platform-i8042-serio-0-event-kbd";
struct input_event ev;
ssize_t n;
int fd;
fd = open(dev, O_RDONLY);
if (fd == -1) {
fprintf(stderr, "Cannot open %s: %s.\n", dev, strerror(errno));
return EXIT_FAILURE;
}
while (1) {
n = read(fd, &ev, sizeof(ev));
if (n == (ssize_t)-1) {
if (errno == EINTR)
continue;
else
break;
}
else {
if (n != sizeof(ev)) {
errno = EIO;
break;
}
if (ev.type == EV_KEY && ev.value >= 0 && ev.value < 2 && ev.code == 58)
printf("%s CPASLOCK\n", evval[ev.value]);
}
}
fflush(stdout);
fprintf(stderr, "%s.\n", strerror(errno));
return EXIT_FAILURE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment