diff options
author | cflip <cflip@cflip.net> | 2023-01-09 15:47:26 -0700 |
---|---|---|
committer | cflip <cflip@cflip.net> | 2023-01-09 15:47:26 -0700 |
commit | c1573dbbb3e70cad8c1f94cbed5b2abf2b124ace (patch) | |
tree | 8333d29b92f12f7cbf97deb38e25875887d6b19a | |
parent | 54d8fdd290e21ae8c773b4228827ee7539637ce8 (diff) |
Implement basic keyboard event handling
-rw-r--r-- | input.c | 5 | ||||
-rw-r--r-- | input.h | 2 | ||||
-rw-r--r-- | main.c | 2 | ||||
-rw-r--r-- | window.c | 42 | ||||
-rw-r--r-- | window.h | 2 |
5 files changed, 45 insertions, 8 deletions
@@ -97,10 +97,9 @@ void editor_move_cursor(struct editor_state* editor, int key) editor->cursor_x = row_length; } -void editor_process_keypress(struct editor_state* editor) +void editor_process_keypress(struct editor_state* editor, int c) { static int quit_message = 1; - int c = editor_read_key(); switch (c) { case '\r': @@ -112,8 +111,6 @@ void editor_process_keypress(struct editor_state* editor) quit_message = 0; return; } - write(STDOUT_FILENO, "\x1b[2J", 4); - write(STDOUT_FILENO, "\x1b[H", 3); exit(0); break; case CTRL_KEY('s'): @@ -20,6 +20,6 @@ enum editor_key { int editor_read_key(); void editor_move_cursor(struct editor_state* editor, int key); -void editor_process_keypress(struct editor_state* editor); +void editor_process_keypress(struct editor_state* editor, int keycode); #endif @@ -16,7 +16,7 @@ int main(int argc, char** argv) editor_set_status_message(&editor, "HELP: Ctrl+Q = quit, Ctrl+S = save, Ctrl+F = find"); - while (window_handle_event()) { + while (window_handle_event(&editor)) { window_redraw(&editor); // editor_refresh_screen(&editor); // editor_process_keypress(&editor); @@ -5,6 +5,7 @@ #include "buffer.h" #include "editor.h" #include "font.h" +#include "input.h" static SDL_Window *window = NULL; static SDL_Renderer *renderer = NULL; @@ -39,13 +40,52 @@ void window_init() SDL_ShowWindow(window); } -int window_handle_event() +int window_handle_event(struct editor_state *editor) { static SDL_Event e; SDL_WaitEvent(&e); switch (e.type) { case SDL_QUIT: return 0; + case SDL_KEYDOWN: + int keycode = e.key.keysym.sym; + switch (e.key.keysym.sym) { + case SDLK_BACKSPACE: + keycode = BACKSPACE; + break; + case SDLK_LEFT: + keycode = ARROW_LEFT; + break; + case SDLK_RIGHT: + keycode = ARROW_RIGHT; + break; + case SDLK_UP: + keycode = ARROW_UP; + break; + case SDLK_DOWN: + keycode = ARROW_DOWN; + break; + case SDLK_DELETE: + keycode = DELETE_KEY; + break; + case SDLK_HOME: + keycode = HOME_KEY; + break; + case SDLK_END: + keycode = END_KEY; + break; + case SDLK_PAGEUP: + keycode = PAGE_UP; + break; + case SDLK_PAGEDOWN: + keycode = PAGE_DOWN; + break; + } + if (e.key.keysym.mod & KMOD_CTRL) { + keycode = CTRL_KEY(keycode); + } + editor_process_keypress(editor, keycode); + break; } return 1; } @@ -4,7 +4,7 @@ struct editor_state; void window_init(); -int window_handle_event(); +int window_handle_event(struct editor_state *editor); void window_redraw(struct editor_state *editor); void window_destroy(); |