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.
This commit is contained in:
kichikuou
2026-05-03 07:13:47 +09:00
parent 4c4a62e1ed
commit e83cb1c91e
3 changed files with 28 additions and 4 deletions
+2
View File
@@ -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 */
+8
View File
@@ -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)
+18 -4
View File
@@ -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();