From a44225c625101355d0db66b8181ce71692c9f740 Mon Sep 17 00:00:00 2001 From: Daniel Gibson Date: Fri, 10 Apr 2020 02:39:09 +0200 Subject: [PATCH] Introduce K_CONSOLE, generated by the keyboard's "console key" regardless of keyboard layout, with a special exemption for layouts where that key generates a quote character (like the Brazilian one) because you may wanna type a quote into the console. (It's SDL_SCANCODE_GRAVE, that key between Esc, 1 and Tab) The old hack of matching for ^, ~ and ` in Char_Event() didn't work very well for layouts we didn't anticipate, which is especially relevant with the recent Scancode fallback, which for example allows binding the ^ key on Belgian keyboards (which is on SDL_SCANCODE_LEFTBRACKET, far away from the "console key"), but in that case would *also* open the console. This is mostly straight-forward, except for a small hack to prevent the key from generating text input (on German layouts you otherwise get "^" in the console when closing+opening it), which requires the "console key" to be pressed without any modifiers like Shift or AltGr. Yes, it's ugly, but it works and all the uglyness is contained in IN_Update() and on the other hand Char_Event() becomes less ugly :) --- src/client/cl_keyboard.c | 11 ++--------- src/client/header/keyboard.h | 8 +++++++- src/client/input/sdl.c | 29 ++++++++++++++++++++++++++--- 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/src/client/cl_keyboard.c b/src/client/cl_keyboard.c index 6fde0d59..f1def4a1 100644 --- a/src/client/cl_keyboard.c +++ b/src/client/cl_keyboard.c @@ -1091,13 +1091,6 @@ Key_Init(void) void Char_Event(int key) { - /* console key is hardcoded, so the user can never unbind it */ - if ((key == '^') || (key == '~') || (key == '`')) - { - Con_ToggleConsole_f(); - return; - } - switch (cls.key_dest) { /* Chat */ @@ -1202,8 +1195,8 @@ Key_Event(int key, qboolean down, qboolean special) return; } - /* Toogle console though Shift + Escape */ - if (down && keydown[K_SHIFT] && key == K_ESCAPE) + /* Toogle console through Shift + Escape or special K_CONSOLE key */ + if ( down && ( key == K_CONSOLE || (keydown[K_SHIFT] && key == K_ESCAPE) ) ) { Con_ToggleConsole_f(); return; diff --git a/src/client/header/keyboard.h b/src/client/header/keyboard.h index 95a766e4..24870135 100644 --- a/src/client/header/keyboard.h +++ b/src/client/header/keyboard.h @@ -265,7 +265,7 @@ enum QKEYS { K_SC_NONUSHASH, K_SC_SEMICOLON, K_SC_APOSTROPHE, - K_SC_GRAVE, // console key + K_SC_GRAVE, K_SC_COMMA, K_SC_PERIOD, K_SC_SLASH, @@ -285,6 +285,12 @@ enum QKEYS { K_SC_CURRENCYUNIT, K_SC_CURRENCYSUBUNIT, + // hardcoded pseudo-key to open the console, emitted when pressing the "console key" + // (SDL_SCANCODE_GRAVE, the one between Esc, 1 and Tab) on layouts that don't + // have a relevant char there (unlike Brazilian which has quotes there which you + // want to be able to type in the console) - the user can't bind this key. + K_CONSOLE, + K_LAST }; diff --git a/src/client/input/sdl.c b/src/client/input/sdl.c index d750d197..1f6f00fd 100644 --- a/src/client/input/sdl.c +++ b/src/client/input/sdl.c @@ -455,6 +455,8 @@ IN_Update(void) static qboolean left_trigger = false; static qboolean right_trigger = false; + static int consoleKeyCode = 0; + /* Get and process an event */ while (SDL_PollEvent(&event)) { @@ -501,10 +503,15 @@ IN_Update(void) break; case SDL_TEXTINPUT: - if ((event.text.text[0] >= ' ') && (event.text.text[0] <= '~')) + { + int c = event.text.text[0]; + // also make sure we don't get the char that corresponds to the + // "console key" (like "^" or "`") as text input + if ((c >= ' ') && (c <= '~') && c != consoleKeyCode) { - Char_Event(event.text.text[0]); + Char_Event(c); } + } break; @@ -535,7 +542,23 @@ IN_Update(void) else { SDL_Keycode kc = event.key.keysym.sym; - if ((kc >= SDLK_SPACE) && (kc < SDLK_DELETE)) + if(sc == SDL_SCANCODE_GRAVE && kc != '\'' && kc != '"') + { + // special case/hack: open the console with the "console key" + // (beneath Esc, left of 1, above Tab) + // but not if the keycode for this is a quote (like on Brazilian + // keyboards) - otherwise you couldn't type them in the console + if((event.key.keysym.mod & (KMOD_CAPS|KMOD_SHIFT|KMOD_ALT|KMOD_CTRL|KMOD_GUI)) == 0) + { + // also, only do this if no modifiers like shift or AltGr or whatever are pressed + // so kc will most likely be the ascii char generated by this and can be ignored + // in case SDL_TEXTINPUT above (so we don't get ^ or whatever as text in console) + // (can't just check for mod == 0 because numlock is a KMOD too) + Key_Event(K_CONSOLE, down, true); + consoleKeyCode = kc; + } + } + else if ((kc >= SDLK_SPACE) && (kc < SDLK_DELETE)) { Key_Event(kc, down, false); }