From 60585faf7951cffb6046280bdd5f91275fb97d65 Mon Sep 17 00:00:00 2001 From: Jack Jamison Date: Sun, 12 Oct 2025 00:07:02 -0400 Subject: input buffer and key repeat --- input_field.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 input_field.c (limited to 'input_field.c') diff --git a/input_field.c b/input_field.c new file mode 100644 index 0000000..026a22c --- /dev/null +++ b/input_field.c @@ -0,0 +1,49 @@ +#include + +#include "input_field.h" +#include "array.h" + +void type_key(client_state* state, xkb_keysym_t keysym) { + bool ctrl = xkb_state_mod_name_is_active(state->xkb_state, XKB_MOD_NAME_CTRL, XKB_STATE_MODS_EFFECTIVE); + + // exit app + if ((keysym == XKB_KEY_c && ctrl) || + (keysym == XKB_KEY_g && ctrl) || + (keysym == XKB_KEY_Escape)) { + + state->running = false; + return; + } + + // submit line + if (keysym == XKB_KEY_Return || keysym == XKB_KEY_KP_Enter) { + submit_line(state); + return; + } + + // delete + if (keysym == XKB_KEY_BackSpace || keysym == XKB_KEY_Delete) { + if (ctrl) { + array_clear(state->input_buffer); + } else { + array_pop(state->input_buffer); + } + return; + } + + // type char into buffer + char buf[16] = {0}; + int len = xkb_keysym_to_utf8(keysym, buf, sizeof(buf)); + if (len > 0) { + for (int i = 0; i < strlen(buf); i++) { + array_add(state->input_buffer, buf[i]); + } + } +} + +void submit_line(client_state* state) { + if (state->input_buffer) { + printf("%.*s\n", (int)array_size(state->input_buffer), state->input_buffer); + array_clear(state->input_buffer); + } +} -- cgit v1.2.3