Only do work to process text events if text input is active

Fixes https://github.com/libsdl-org/SDL/issues/9353
This commit is contained in:
Sam Lantinga 2024-03-25 12:49:09 -07:00
parent 658f3cdcf1
commit fa236f169b
6 changed files with 105 additions and 79 deletions

View file

@ -1180,6 +1180,10 @@ int SDL_SendKeyboardText(const char *text)
SDL_Keyboard *keyboard = &SDL_keyboard;
int posted;
if (!SDL_TextInputActive()) {
return 0;
}
/* Don't post text events for unprintable characters */
if (SDL_iscntrl((unsigned char)*text)) {
return 0;
@ -1210,6 +1214,10 @@ int SDL_SendEditingText(const char *text, int start, int length)
SDL_Keyboard *keyboard = &SDL_keyboard;
int posted;
if (!SDL_TextInputActive()) {
return 0;
}
/* Post the event, if desired */
posted = 0;
if (SDL_EventEnabled(SDL_EVENT_TEXT_EDITING)) {

View file

@ -837,11 +837,14 @@ static EM_BOOL Emscripten_HandleKey(int eventType, const EmscriptenKeyboardEvent
static EM_BOOL Emscripten_HandleKeyPress(int eventType, const EmscriptenKeyboardEvent *keyEvent, void *userData)
{
if (SDL_TextInputActive()) {
char text[5];
if (Emscripten_ConvertUTF32toUTF8(keyEvent->charCode, text)) {
SDL_SendKeyboardText(text);
}
return SDL_EventEnabled(SDL_EVENT_TEXT_INPUT);
return EM_TRUE;
}
return EM_FALSE;
}
static EM_BOOL Emscripten_HandleFullscreenChange(int eventType, const EmscriptenFullscreenChangeEvent *fullscreenChangeEvent, void *userData)

View file

@ -1605,7 +1605,9 @@ static void keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Wayland_UpdateImplicitGrabSerial(input, serial);
if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
if (SDL_TextInputActive()) {
has_text = keyboard_input_get_text(text, input, key, SDL_PRESSED, &handled_by_ime);
}
} else {
if (keyboard_repeat_key_is_set(&input->keyboard_repeat, key)) {
/* Send any due key repeat events before stopping the repeat and generating the key up event.

View file

@ -1164,21 +1164,25 @@ LRESULT CALLBACK WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lPara
if (wParam == UNICODE_NOCHAR) {
returnCode = 1;
} else {
if (SDL_TextInputActive()) {
char text[5];
if (SDL_UCS4ToUTF8((Uint32)wParam, text) != text) {
SDL_SendKeyboardText(text);
}
}
returnCode = 0;
}
break;
case WM_CHAR:
if (SDL_TextInputActive()) {
/* Characters outside Unicode Basic Multilingual Plane (BMP)
* are coded as so called "surrogate pair" in two separate UTF-16 character events.
* Cache high surrogate until next character event. */
if (IS_HIGH_SURROGATE(wParam)) {
data->high_surrogate = (WCHAR)wParam;
} else {
if (SDL_TextInputActive()) {
WCHAR utf16[3];
utf16[0] = data->high_surrogate ? data->high_surrogate : (WCHAR)wParam;
@ -1190,7 +1194,10 @@ LRESULT CALLBACK WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lPara
if (result > 0) {
SDL_SendKeyboardText(utf8);
}
}
data->high_surrogate = L'\0';
}
} else {
data->high_surrogate = L'\0';
}

View file

@ -88,6 +88,7 @@ void WINRT_ProcessCharacterReceivedEvent(SDL_Window *window, Windows::UI::Core::
SDL_WindowData *data = window->driverdata;
if (SDL_TextInputActive()) {
/* Characters outside Unicode Basic Multilingual Plane (BMP)
* are coded as so called "surrogate pair" in two separate UTF-16 character events.
* Cache high surrogate until next character event. */
@ -107,6 +108,9 @@ void WINRT_ProcessCharacterReceivedEvent(SDL_Window *window, Windows::UI::Core::
SDL_SendKeyboardText(utf8);
}
data->high_surrogate = L'\0';
}
} else {
data->high_surrogate = L'\0';
}
}

View file

@ -842,6 +842,23 @@ void X11_HandleKeyEvent(SDL_VideoDevice *_this, SDL_WindowData *windowdata, SDL_
Status status = 0;
SDL_bool handled_by_ime = SDL_FALSE;
#ifdef DEBUG_XEVENTS
printf("window %p: %s (X11 keycode = 0x%X)\n", data, (xevent->type == KeyPress ? "KeyPress" : "KeyRelease"), xevent->xkey.keycode);
#endif
#ifdef DEBUG_SCANCODES
if (videodata->key_layout[keycode] == SDL_SCANCODE_UNKNOWN && keycode) {
int min_keycode, max_keycode;
X11_XDisplayKeycodes(display, &min_keycode, &max_keycode);
keysym = X11_KeyCodeToSym(_this, keycode, xevent->xkey.state >> 13);
SDL_Log("The key you just pressed is not recognized by SDL. To help get this fixed, please report this to the SDL forums/mailing list <https://discourse.libsdl.org/> X11 KeyCode %d (%d), X11 KeySym 0x%lX (%s).\n",
keycode, keycode - min_keycode, keysym,
X11_XKeysymToString(keysym));
}
#endif /* DEBUG SCANCODES */
text[0] = '\0';
if (SDL_TextInputActive()) {
/* Save the original keycode for dead keys, which are filtered out by
the XFilterEvent() call below.
*/
@ -871,21 +888,6 @@ void X11_HandleKeyEvent(SDL_VideoDevice *_this, SDL_WindowData *windowdata, SDL_
return;
}
#ifdef DEBUG_XEVENTS
printf("window %p: %s (X11 keycode = 0x%X)\n", data, (xevent->type == KeyPress ? "KeyPress" : "KeyRelease"), xevent->xkey.keycode);
#endif
#ifdef DEBUG_SCANCODES
if (videodata->key_layout[keycode] == SDL_SCANCODE_UNKNOWN && keycode) {
int min_keycode, max_keycode;
X11_XDisplayKeycodes(display, &min_keycode, &max_keycode);
keysym = X11_KeyCodeToSym(_this, keycode, xevent->xkey.state >> 13);
SDL_Log("The key you just pressed is not recognized by SDL. To help get this fixed, please report this to the SDL forums/mailing list <https://discourse.libsdl.org/> X11 KeyCode %d (%d), X11 KeySym 0x%lX (%s).\n",
keycode, keycode - min_keycode, keysym,
X11_XKeysymToString(keysym));
}
#endif /* DEBUG SCANCODES */
SDL_zeroa(text);
#ifdef X_HAVE_UTF8_STRING
if (windowdata->ic && xevent->type == KeyPress) {
X11_Xutf8LookupString(windowdata->ic, &xevent->xkey, text, sizeof(text),
@ -898,10 +900,10 @@ void X11_HandleKeyEvent(SDL_VideoDevice *_this, SDL_WindowData *windowdata, SDL_
#endif
#ifdef SDL_USE_IME
if (SDL_TextInputActive()) {
handled_by_ime = SDL_IME_ProcessKeyEvent(keysym, keycode, (xevent->type == KeyPress ? SDL_PRESSED : SDL_RELEASED));
}
#endif
}
if (!handled_by_ime) {
if (xevent->type == KeyPress) {
/* Don't send the key if it looks like a duplicate of a filtered key sent by an IME */