Created
August 27, 2020 13:33
-
-
Save renke/e08a6822df0e26b314288378db9d770a to your computer and use it in GitHub Desktop.
This file contains 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
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <linux/input.h> | |
int read_event(struct input_event *event) | |
{ | |
return fread(event, sizeof(struct input_event), 1, stdin) == 1; | |
} | |
void write_event(const struct input_event *event) | |
{ | |
if (fwrite(event, sizeof(struct input_event), 1, stdout) != 1) | |
exit(EXIT_FAILURE); | |
} | |
int main(void) | |
{ | |
int use_arrow_keys = 0; | |
struct input_event input; | |
setbuf(stdin, NULL), setbuf(stdout, NULL); | |
while (read_event(&input)) | |
{ | |
if (input.type == EV_MSC && input.code == MSC_SCAN) | |
continue; | |
if (input.type != EV_KEY) | |
{ | |
write_event(&input); | |
continue; | |
} | |
if (use_arrow_keys) | |
{ | |
if (input.code == KEY_LEFTCTRL) | |
{ | |
if (input.value == 0) { | |
use_arrow_keys = 0; | |
continue; | |
} | |
if (input.value == 1 || input.value == 2) { | |
continue; | |
} | |
} | |
} | |
else | |
{ | |
if (input.code == KEY_LEFTCTRL) | |
{ | |
if (input.value == 1 || input.value == 2) { | |
use_arrow_keys = 1; | |
continue; | |
} | |
if (input.value == 0) { | |
continue; | |
} | |
} | |
} | |
if (use_arrow_keys) { | |
// E → Up | |
if (input.code == KEY_E) { | |
input.code = KEY_UP; | |
} | |
// D → Down | |
if (input.code == KEY_D) { | |
input.code = KEY_DOWN; | |
} | |
// S → Left | |
if (input.code == KEY_S) { | |
input.code = KEY_LEFT; | |
} | |
// F → Right | |
if (input.code == KEY_F) { | |
input.code = KEY_RIGHT; | |
} | |
// A → Home | |
if (input.code == KEY_A) { | |
input.code = KEY_HOME; | |
} | |
// G → End | |
if (input.code == KEY_G) { | |
input.code = KEY_END; | |
} | |
// C → Page Down | |
if (input.code == KEY_C) { | |
input.code = KEY_PAGEDOWN; | |
} | |
// 4 → Page Up | |
if (input.code == KEY_4) { | |
input.code = KEY_PAGEUP; | |
} | |
} | |
// ALT → LEFT CTRL | |
if (input.code == KEY_LEFTALT) | |
{ | |
input.code = KEY_LEFTCTRL; | |
} | |
// CAPSLOCK → ESCAPE | |
if (input.code == KEY_CAPSLOCK) | |
{ | |
input.code = KEY_ESC; | |
} | |
// RIGHT ALT → LEFT ALT | |
if (input.code == KEY_RIGHTALT) | |
{ | |
input.code = KEY_LEFTALT; | |
} | |
// 102ND → COMPOSE | |
if (input.code == KEY_102ND) | |
{ | |
input.code = KEY_COMPOSE; | |
} | |
write_event(&input); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment