Skip to content

Instantly share code, notes, and snippets.

@errzey
Created July 4, 2010 05:34
Show Gist options
  • Save errzey/463175 to your computer and use it in GitHub Desktop.
Save errzey/463175 to your computer and use it in GitHub Desktop.
void
process_keystroke(int fd, short which, keyboard_t *kb)
{
struct input_event input_event;
ssize_t bytes_read;
char key;
bytes_read = read(fd, &input_event, sizeof(struct input_event));
if (bytes_read != sizeof(struct input_event)) {
fprintf(stderr, "[*] error reading device, ABORTING!\n");
return;
}
/* make sure this is a keypress of some sort */
if (input_event.type != EV_KEY) {
return;
}
/* look for some things that may modify the next stroke */
switch (input_event.code) {
case KEY_RIGHTSHIFT:
case KEY_LEFTSHIFT:
kb->shift_mod = input_event.value;
return;
case KEY_RIGHTCTRL:
case KEY_LEFTCTRL:
/* I don't really care about ctrl chars */
return;
case KEY_CAPSLOCK:
if (input_event.value == 1) {
if (kb->caps_mod) {
kb->caps_mod = 0;
} else {
kb->caps_mod = 1;
}
}
return;
}
if (input_event.value != 1) {
return;
}
if (input_event.code > sizeof(lowercase_map)) {
return;
}
if (kb->shift_mod) {
key = uppercase_map[input_event.code];
if (kb->caps_mod) {
key = tolower(key);
}
} else {
key = lowercase_map[input_event.code];
if (kb->caps_mod) {
key = toupper(key);
}
}
fprintf(stdout, "%c", key ? key : 0);
fflush(stdout);
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment