From d00e6740883d6054eda846e0fb249397d3f58c42 Mon Sep 17 00:00:00 2001 From: NepDisk Date: Sat, 13 Sep 2025 22:41:48 -0400 Subject: [PATCH] Port Native keyboard --- src/console.c | 3 +++ src/i_video.h | 2 ++ src/m_menu.c | 2 ++ src/m_menu.h | 2 ++ src/m_textinput.c | 2 +- src/sdl/i_video.cpp | 37 ++++++++++++++++++++++++++++++++++++- 6 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/console.c b/src/console.c index 838480066..1eb7b1e43 100644 --- a/src/console.c +++ b/src/console.c @@ -677,6 +677,9 @@ static void CON_MoveConsole(void) INT32 CON_ShiftChar(INT32 ch) { + if (I_UseNativeKeyboard()) + return ch; + if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) { if (shiftdown ^ capslock) diff --git a/src/i_video.h b/src/i_video.h index b35e2c997..cb44c4508 100644 --- a/src/i_video.h +++ b/src/i_video.h @@ -153,6 +153,8 @@ void I_EndRead(void); UINT32 I_GetRefreshRate(void); +boolean I_UseNativeKeyboard(void); + void I_SetBorderlessWindow(void); #ifdef __cplusplus diff --git a/src/m_menu.c b/src/m_menu.c index 79854271d..f2f0fa8d9 100644 --- a/src/m_menu.c +++ b/src/m_menu.c @@ -146,6 +146,7 @@ const char *quitmsg[NUM_QUITMESSAGES]; boolean fromlevelselect = false; +boolean menu_text_input = false; static char menu_text_input_buf[MAXSTRINGLENGTH]; static textinput_t menuinput; @@ -1634,6 +1635,7 @@ boolean M_Responder(event_t *ev) if (M_TextInputHandle(&menuinput, ch)) { + menu_text_input = true; S_StartSound(NULL, sfx_menu1); // Tails CV_Set(item->cvar, menuinput.buffer); return true; diff --git a/src/m_menu.h b/src/m_menu.h index b3f2b8cc5..82fc059fe 100644 --- a/src/m_menu.h +++ b/src/m_menu.h @@ -490,6 +490,8 @@ UINT16 M_GetColorAfter(UINT16 color); void M_InitPlayerSetupColors(void); void M_FreePlayerSetupColors(void); +extern boolean menu_text_input; + #ifdef __cplusplus } // extern "C" #endif diff --git a/src/m_textinput.c b/src/m_textinput.c index 94a087ff8..8637dc929 100644 --- a/src/m_textinput.c +++ b/src/m_textinput.c @@ -382,7 +382,7 @@ static boolean M_TextInputHandleBase(textinput_t *input, INT32 key, boolean emot key = '/'; // same capslock code as hu_stuff.c's HU_responder. Check there for details. - key = /*cv_keyboardlayout.value == 3 ? CON_ShitAndAltGrChar(key) : */CON_ShiftChar(key); + key = CON_ShiftChar(key); // enter a char into the command prompt if (key < 32 || key > 127) diff --git a/src/sdl/i_video.cpp b/src/sdl/i_video.cpp index 9eace8973..28bf4c54e 100644 --- a/src/sdl/i_video.cpp +++ b/src/sdl/i_video.cpp @@ -501,6 +501,40 @@ static INT32 Impl_SDL_Scancode_To_Keycode(SDL_Scancode code) return 0; } +boolean I_UseNativeKeyboard(void) +{ + return (chat_on || CON_Ready() || (menu_text_input && menustack[0])); +} + +// Get the equivalent ASCII (Unicode?) character for a keypress. +static INT32 GetTypedChar(SDL_Keysym keysym) +{ + SDL_Event next_event; + SDL_Keycode keycode = keysym.sym; + SDL_Scancode scancode = keysym.scancode; + const boolean Text_Input_Only = I_UseNativeKeyboard(); // only use this this if on chat or console or the current menu wants inputs from us (except if its the control setup menu ig) + + // Special cases, where we always return a fixed value. + switch (keycode) + { + case SDLK_BACKSPACE: return KEY_BACKSPACE; + case SDLK_RETURN: return KEY_ENTER; + default: + break; + } + + if (Text_Input_Only) + { + if (SDL_PeepEvents(&next_event, 1, SDL_PEEKEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT) == 1 && next_event.type == SDL_TEXTINPUT) + { + if (next_event.text.text[1] == '\0') // limit to ASCII + return next_event.text.text[0]; + } + } + + return Impl_SDL_Scancode_To_Keycode(scancode); // fallback +} + static boolean IgnoreMouse(void) { if (cv_alwaysgrabmouse.value) @@ -699,7 +733,8 @@ static void Impl_HandleKeyboardEvent(SDL_KeyboardEvent evt, Uint32 type) { return; } - event.data1 = Impl_SDL_Scancode_To_Keycode(evt.keysym.scancode); + + event.data1 = GetTypedChar(evt.keysym); if (event.data1) D_PostEvent(&event); }