Add keylog.c

This commit is contained in:
Lluciocc 2026-04-17 01:05:18 +02:00 committed by GitHub
parent 9b6297c917
commit d677d37b1c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

62
src/userland/gui/keylog.c Normal file
View file

@ -0,0 +1,62 @@
#include "libc/syscall.h"
#include "libc/libui.h"
#include "libc/stdlib.h"
#include <stdint.h>
#include <stdbool.h>
/*
@Lluciocc
The most SIMPLE keylogger for debug
FEAT:
- Log every key pressed in the terminal and gives their keycode
*/
#define WINDOW_W 400
#define WINDOW_H 200
static int last_key = -1;
static void draw(ui_window_t win) {
ui_draw_rect(win, 0, 0, WINDOW_W, WINDOW_H, 0xFF121212);
ui_draw_string(win, 20, 20, "Key Logger", 0xFFFFFFFF);
if (last_key >= 0) {
char buf[32];
itoa(last_key, buf);
ui_draw_string(win, 20, 60, "Last key:", 0xFFAAAAAA);
ui_draw_string(win, 20, 90, buf, 0xFF6EA8FE);
} else {
ui_draw_string(win, 20, 60, "Press any key...", 0xFFAAAAAA);
}
}
int main(void) {
ui_window_t win = ui_window_create("keylog", 100, 100, WINDOW_W, WINDOW_H);
if (!win) return 1;
gui_event_t ev;
while (1) {
if (ui_get_event(win, &ev)) {
if (ev.type == GUI_EVENT_KEY) {
last_key = ev.arg1;
printf("Key pressed: %d\n", ev.arg1);
}
if (ev.type == GUI_EVENT_CLOSE) {
sys_exit(0);
}
draw(win);
ui_mark_dirty(win, 0, 0, WINDOW_W, WINDOW_H);
} else {
sys_yield();
}
}
return 0;
}