diff --git a/src/userland/games/2048.c b/src/userland/games/2048.c index 3700d25..2b032a5 100644 --- a/src/userland/games/2048.c +++ b/src/userland/games/2048.c @@ -1,6 +1,7 @@ #include "libc/syscall.h" #include "libc/libui.h" #include "libc/stdlib.h" +#include "libc/input.h" #include #include #include @@ -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) { diff --git a/src/userland/gui/keylog.c b/src/userland/gui/keylog.c new file mode 100644 index 0000000..a5f5422 --- /dev/null +++ b/src/userland/gui/keylog.c @@ -0,0 +1,62 @@ +#include "libc/syscall.h" +#include "libc/libui.h" +#include "libc/stdlib.h" +#include +#include + +/* +@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; +} diff --git a/src/userland/libc/input.h b/src/userland/libc/input.h new file mode 100644 index 0000000..e5c05b6 --- /dev/null +++ b/src/userland/libc/input.h @@ -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