From e83cb1c91ec3117604c36abc9dabd1a24dff4d62 Mon Sep 17 00:00:00 2001 From: kichikuou Date: Mon, 27 Apr 2026 07:09:40 +0900 Subject: [PATCH] InputString: Handle Backspace key to delete last character This adds a key handler facility to input.c so that the InputString can intercept the Backspace key while the IME is open, and remove the last character from the result string. Backspace is ignored while the IME has composing text, so it does not interfere with IME editing. --- include/input.h | 2 ++ src/hll/InputString.c | 8 ++++++++ src/input.c | 22 ++++++++++++++++++---- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/include/input.h b/include/input.h index cd0cbee..75a8f67 100644 --- a/include/input.h +++ b/include/input.h @@ -153,5 +153,7 @@ void register_input_handler(void(*handler)(const char*)); void clear_input_handler(void); void register_editing_handler(void(*handler)(const char*, int, int)); void clear_editing_handler(void); +void register_key_handler(void(*handler)(int)); +void clear_key_handler(void); #endif /* SYSTEM4_INPUT_H */ diff --git a/src/hll/InputString.c b/src/hll/InputString.c index 4605e5c..f1839ca 100644 --- a/src/hll/InputString.c +++ b/src/hll/InputString.c @@ -95,10 +95,17 @@ static void handle_editing(const char *text, int start, int length) has_editing_text = *text != '\0'; } +static void handle_key(int code) +{ + if (code == VK_BACK && !has_editing_text && result && result->size > 0) + string_pop_back(&result); +} + static void InputString_OpenIME(void) { register_input_handler(handle_input); register_editing_handler(handle_editing); + register_key_handler(handle_key); has_editing_text = false; SDL_StartTextInput(); } @@ -109,6 +116,7 @@ static void InputString_CloseIME(void) has_editing_text = false; clear_input_handler(); clear_editing_handler(); + clear_key_handler(); } static bool InputString_Inputs(void) diff --git a/src/input.c b/src/input.c index 709c748..e0a6a89 100644 --- a/src/input.c +++ b/src/input.c @@ -232,13 +232,20 @@ bool mouse_show_cursor(bool show) return SDL_ShowCursor(show ? SDL_ENABLE : SDL_DISABLE) >= 0; } +static void(*input_handler)(const char*); +static void(*editing_handler)(const char*, int, int); +static void(*key_handler)(int); + static void key_event(SDL_KeyboardEvent *e, bool pressed) { if (e->keysym.scancode >= (sizeof(sdl_keytable)/sizeof(*sdl_keytable))) return; enum sact_keycode code = sdl_keytable[e->keysym.scancode]; - if (code) + if (code) { key_state[code] = pressed; + if (pressed && key_handler) + key_handler(code); + } } static void mouse_event(SDL_MouseButtonEvent *e) @@ -496,9 +503,6 @@ static void controller_button_event(SDL_ControllerButtonEvent *e) joybutton_state[e->button] = e->state == SDL_PRESSED; } -static void(*input_handler)(const char*); -static void(*editing_handler)(const char*, int, int); - void register_input_handler(void(*handler)(const char*)) { input_handler = handler; @@ -519,6 +523,16 @@ void clear_editing_handler(void) editing_handler = NULL; } +void register_key_handler(void(*handler)(int)) +{ + key_handler = handler; +} + +void clear_key_handler(void) +{ + key_handler = NULL; +} + static void fire_deferred_events(void) { uint32_t now = SDL_GetTicks();