From a455e9884fb7e9decfd02e0f5f921d3b21bcf702 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Thomas?= Date: Fri, 7 Apr 2017 01:35:00 +0200 Subject: [PATCH] Map SDL_SCANCODE_GRAVE to the console key. Currently, the only way to invoke the console which is both platform and layout independent is to use Shift+Escape. This change makes sure the QWERTY '`' key (at the left of the 1 key) works as a console key and for all keyboard layout by matching against its scan code (ie physical location). Since this key can also be mapped to normal characters that could be useful for text input (eg, '^' on German keyboards), we only interpret it as a console key if one of those conditions is respected: - The resulting key is valid and outside ascii range (0x20-0x7f) - The resulting key is '~' or '`' - The console input field is empty and no chat message is being written Additionaly, no modifiers (Shift, Ctrl, Alt, AltGr, Ctrl, Super/Win) shall be pressed to allow usage of the keys in the Shift/AltGr group and to allow bypassing the console (Alt works on X11, Windows key must be used on Windows) A new `cl_consoleUseScanCode` cvar (default 1) is added to enable the feature. --- code/client/cl_main.c | 2 ++ code/client/client.h | 1 + code/sdl/sdl_input.c | 26 ++++++++++++++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/code/client/cl_main.c b/code/client/cl_main.c index 31dd9ab782..4a8369c85b 100644 --- a/code/client/cl_main.c +++ b/code/client/cl_main.c @@ -119,6 +119,7 @@ cvar_t *cl_lanForcePackets; cvar_t *cl_guidServerUniq; cvar_t *cl_consoleKeys; +cvar_t *cl_consoleUseScanCode; cvar_t *cl_rate; @@ -3607,6 +3608,7 @@ void CL_Init( void ) { // ~ and `, as keys and characters cl_consoleKeys = Cvar_Get( "cl_consoleKeys", "~ ` 0x7e 0x60", CVAR_ARCHIVE); + cl_consoleUseScanCode = Cvar_Get( "cl_consoleUseScanCode", "1", CVAR_ARCHIVE); // userinfo Cvar_Get ("name", "UnnamedPlayer", CVAR_USERINFO | CVAR_ARCHIVE ); diff --git a/code/client/client.h b/code/client/client.h index 0fe3889c4e..1645ce6ad9 100644 --- a/code/client/client.h +++ b/code/client/client.h @@ -423,6 +423,7 @@ extern cvar_t *cl_lanForcePackets; extern cvar_t *cl_autoRecordDemo; extern cvar_t *cl_consoleKeys; +extern cvar_t *cl_consoleUseScanCode; #ifdef USE_MUMBLE extern cvar_t *cl_useMumble; diff --git a/code/sdl/sdl_input.c b/code/sdl/sdl_input.c index 4c8ff1feea..924ca5205f 100644 --- a/code/sdl/sdl_input.c +++ b/code/sdl/sdl_input.c @@ -291,6 +291,32 @@ static keyNum_t IN_TranslateSDLToQ3Key( SDL_Keysym *keysym, qboolean down ) { // Console keys can't be bound or generate characters key = K_CONSOLE; + } else if ( cl_consoleUseScanCode->integer && keysym->scancode == SDL_SCANCODE_GRAVE ) { + // Use the QWERTY grave key location as a console key (whatever the actual layout) + // We try our best to avoid any possible conflicts with keys at this location + // on other layouts such as: + // ^ (German) + // " (Brazilian) + // $ (French Bepo, Programmer Dvorak) + // 0 (Hungarian) + // ; (Czech) + // | (Italian) + // # (Canadian French) + // and probably others + qboolean console_has_text = strlen(g_consoleField.buffer) > 0; + + // Don't force console if a modifier is held + if ( keys[K_SHIFT].down || keys[K_ALT].down || keys[K_CTRL].down || keys[K_SUPER].down ) { + return key; + } + + // We need to exclude the invalid 0 key because the German layout outputs a dead key + // which is not recognized correctly by SDL under X11 + if ( (key > 0 && key < 0x20) || key > 0x7f || key == '~' || key == '`' + || ( Key_GetCatcher() != KEYCATCH_MESSAGE && !console_has_text ) ) + { + key = K_CONSOLE; + } } return key;