This commit is contained in:
boreddevnl 2026-04-17 09:20:41 +02:00
commit 67ebcb98d1
3 changed files with 86 additions and 15 deletions

View file

@ -1,6 +1,7 @@
#include "libc/syscall.h"
#include "libc/libui.h"
#include "libc/stdlib.h"
#include "libc/input.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
@ -412,31 +413,20 @@ static bool point_in_rect(int px, int py, int x, int y, int w, int h) {
return px >= x && px < x + w && py >= y && py < y + h;
}
/*
37 = left arrow
38 = up arrow
39 = right arrow
40 = down arrow
72 = numpad 8
75 = numpad 4
77 = numpad 6
80 = numpad 2
key = letter
*/
static bool is_left_key(int key) {
return key == 'a' || key == 'A' || key == 37 || key == 75;
return key == 'a' || key == 'A' || key == KEY_LEFT;
}
static bool is_right_key(int key) {
return key == 'd' || key == 'D' || key == 39 || key == 77;
return key == 'd' || key == 'D' || key == KEY_RIGHT;
}
static bool is_up_key(int key) {
return key == 'w' || key == 'W' || key == 38 || key == 72;
return key == 'w' || key == 'W' || key == KEY_UP;
}
static bool is_down_key(int key) {
return key == 's' || key == 'S' || key == 40 || key == 80;
return key == 's' || key == 'S' || key == KEY_DOWN;
}
static void handle_click(int x, int y) {

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;
}

19
src/userland/libc/input.h Normal file
View file

@ -0,0 +1,19 @@
#ifndef INPUT_H
#define INPUT_H
// Arrow keys
#define KEY_UP 17
#define KEY_DOWN 18
#define KEY_LEFT 19
#define KEY_RIGHT 20
// controls
#define KEY_ENTER 10
#define KEY_BACKSPACE 8
#define KEY_ESCAPE 27
#define KEY_SPACE 32
#define KEY_ALT 22
#define KEY_CTRL_L 21
#define KEY_TAB 9
#endif