dhewm3/neo/framework/KeyInput.h
Daniel Gibson ae2d3a7e99 Support (hopefully) all keyboard keys via scancodes, #323
If a key is pressed whichs SDL_Keycode isn't known to Doom3 (has no
corresponding K_* constant), its SDL_Scancode is mapped to the
corresponding newly added K_SC_* scancode constant.
I think I have K_SC_* constants for all keys that differ between
keyboard layouts (which is mostly printable characters; F1-F12, Ctrl,
Shift, ... should be the same on all layouts, which means that e.g.
SDL_SCANCODE_F1 always belongs to SDLK_F1 which the old code already
maps to Doom3's K_F1).
What's extra nice (IMO) is that when Doom3 requests a *localized* name
of the key (like for showing in the bindings menu), we actually use the
name of the SDL_Keycode that *currently* belongs to the scancode, and
esp. the "Western High-ASCII characters" (ISO-8859-1) supported by Doom3
like Ä or Ñ are displayed correctly.

(I already implemented a very similar hack in Yamagi Quake II and
 reused the list of scancodes)

This should fix most of the problems reported in #323
2021-07-15 07:00:18 +02:00

300 lines
7 KiB
C++

/*
===========================================================================
Doom 3 GPL Source Code
Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
This file is part of the Doom 3 GPL Source Code ("Doom 3 Source Code").
Doom 3 Source Code is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Doom 3 Source Code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
===========================================================================
*/
#ifndef __KEYINPUT_H__
#define __KEYINPUT_H__
#include "idlib/CmdArgs.h"
class idFile;
/*
===============================================================================
Key Input
===============================================================================
*/
// these are the key numbers that are used by the key system
// normal keys should be passed as lowercased ascii
// Some high ascii (> 127) characters that are mapped directly to keys on
// western european keyboards are inserted in this table so that those keys
// are bindable (otherwise they get bound as one of the special keys in this
// table)
typedef enum {
// DG: please don't change any existing constants for keyboard keys below (or recreate the tables in win_input.cpp)!
K_TAB = 9,
K_ENTER = 13,
K_ESCAPE = 27,
K_SPACE = 32,
K_BACKSPACE = 127,
K_COMMAND = 128,
K_CAPSLOCK,
K_SCROLL,
K_POWER,
K_PAUSE,
K_UPARROW = 133,
K_DOWNARROW,
K_LEFTARROW,
K_RIGHTARROW,
// The 3 windows keys
K_LWIN = 137,
K_RWIN,
K_MENU,
K_ALT = 140,
K_CTRL,
K_SHIFT,
K_INS,
K_DEL,
K_PGDN,
K_PGUP,
K_HOME,
K_END,
K_F1 = 149,
K_F2,
K_F3,
K_F4,
K_F5,
K_F6,
K_F7,
K_F8,
K_F9,
K_F10,
K_F11,
K_F12,
K_INVERTED_EXCLAMATION = 161, // upside down !
K_F13,
K_F14,
K_F15,
K_KP_HOME = 165,
K_KP_UPARROW,
K_KP_PGUP,
K_KP_LEFTARROW,
K_KP_5,
K_KP_RIGHTARROW,
K_KP_END,
K_KP_DOWNARROW,
K_KP_PGDN,
K_KP_ENTER,
K_KP_INS,
K_KP_DEL,
K_KP_SLASH,
K_SUPERSCRIPT_TWO = 178, // superscript 2
K_KP_MINUS,
K_ACUTE_ACCENT = 180, // accute accent
K_KP_PLUS,
K_KP_NUMLOCK,
K_KP_STAR,
K_KP_EQUALS,
// DG: please don't change any existing constants above this one (or recreate the tables in win_input.cpp)!
K_MASCULINE_ORDINATOR = 186,
// K_MOUSE enums must be contiguous (no char codes in the middle)
K_MOUSE1 = 187,
K_MOUSE2,
K_MOUSE3,
K_MOUSE4,
K_MOUSE5,
K_MOUSE6,
K_MOUSE7,
K_MOUSE8,
K_MWHEELDOWN = 195,
K_MWHEELUP,
K_JOY1 = 197,
K_JOY2,
K_JOY3,
K_JOY4,
K_JOY5,
K_JOY6,
K_JOY7,
K_JOY8,
K_JOY9,
K_JOY10,
K_JOY11,
K_JOY12,
K_JOY13,
K_JOY14,
K_JOY15,
K_JOY16,
K_JOY17,
K_JOY18,
K_JOY19,
K_JOY20,
K_JOY21,
K_JOY22,
K_JOY23,
K_JOY24,
K_JOY25,
K_JOY26,
K_JOY27,
K_GRAVE_A = 224, // lowercase a with grave accent
K_JOY28,
K_JOY29,
K_JOY30,
K_JOY31,
K_JOY32,
K_AUX1 = 230,
K_CEDILLA_C = 231, // lowercase c with Cedilla
K_GRAVE_E = 232, // lowercase e with grave accent
K_AUX2,
K_AUX3,
K_AUX4,
K_GRAVE_I = 236, // lowercase i with grave accent
K_AUX5,
K_AUX6,
K_AUX7,
K_AUX8,
K_TILDE_N = 241, // lowercase n with tilde
K_GRAVE_O = 242, // lowercase o with grave accent
K_AUX9,
K_AUX10,
K_AUX11,
K_AUX12,
K_AUX13,
K_AUX14,
K_GRAVE_U = 249, // lowercase u with grave accent
K_AUX15,
K_AUX16,
K_PRINT_SCR = 252, // SysRq / PrintScr
K_RIGHT_ALT = 253, // used by some languages as "Alt-Gr"
// DG: map all relevant scancodes from SDL to K_SC_* (taken from Yamagi Quake II)
// (relevant are ones that are likely to be keyboardlayout-dependent,
// i.e. printable characters of sorts, *not* Ctrl, Alt, F1, Del, ...)
K_FIRST_SCANCODE = 256,
// !!! NOTE: if you add a scancode here, make sure to also add it to !!!
// !!! scancodemappings[] in sys/events.cpp (and preserve order) !!!
K_SC_A = K_FIRST_SCANCODE,
K_SC_B,
K_SC_C,
K_SC_D,
K_SC_E,
K_SC_F,
K_SC_G,
K_SC_H,
K_SC_I,
K_SC_J,
K_SC_K,
K_SC_L,
K_SC_M,
K_SC_N,
K_SC_O,
K_SC_P,
K_SC_Q,
K_SC_R,
K_SC_S,
K_SC_T,
K_SC_U,
K_SC_V,
K_SC_W,
K_SC_X,
K_SC_Y,
K_SC_Z,
// leaving out SDL_SCANCODE_1 ... _0, we handle them separately already
// also return, escape, backspace, tab, space, already handled as keycodes
K_SC_MINUS,
K_SC_EQUALS,
K_SC_LEFTBRACKET,
K_SC_RIGHTBRACKET,
K_SC_BACKSLASH,
K_SC_NONUSHASH,
K_SC_SEMICOLON,
K_SC_APOSTROPHE,
K_SC_GRAVE,
K_SC_COMMA,
K_SC_PERIOD,
K_SC_SLASH,
// leaving out lots of keys incl. from keypad, we already handle them as normal keys
K_SC_NONUSBACKSLASH,
K_SC_INTERNATIONAL1, /**< used on Asian keyboards, see footnotes in USB doc */
K_SC_INTERNATIONAL2,
K_SC_INTERNATIONAL3, /**< Yen */
K_SC_INTERNATIONAL4,
K_SC_INTERNATIONAL5,
K_SC_INTERNATIONAL6,
K_SC_INTERNATIONAL7,
K_SC_INTERNATIONAL8,
K_SC_INTERNATIONAL9,
K_SC_THOUSANDSSEPARATOR,
K_SC_DECIMALSEPARATOR,
K_SC_CURRENCYUNIT,
K_SC_CURRENCYSUBUNIT,
K_LAST_SCANCODE = K_SC_CURRENCYSUBUNIT, // TODO: keep up to date!
// FIXME: maybe move everything joystick related here
K_LAST_KEY // DG: this said "this better be < 256!"; I hope I fixed all places in code assuming this..
} keyNum_t;
enum { K_NUM_SCANCODES = K_LAST_SCANCODE - K_FIRST_SCANCODE + 1 };
class idKeyInput {
public:
static void Init( void );
static void Shutdown( void );
static void ArgCompletion_KeyName( const idCmdArgs &args, void(*callback)( const char *s ) );
static void PreliminaryKeyEvent( int keyNum, bool down );
static bool IsDown( int keyNum );
static int GetUsercmdAction( int keyNum );
static bool GetOverstrikeMode( void );
static void SetOverstrikeMode( bool state );
static void ClearStates( void );
static int StringToKeyNum( const char *str );
static const char * KeyNumToString( int keyNum, bool localized );
static void SetBinding( int keyNum, const char *binding );
static const char * GetBinding( int keyNum );
static bool UnbindBinding( const char *bind );
static int NumBinds( const char *binding );
static bool ExecKeyBinding( int keyNum );
static const char * KeysFromBinding( const char *bind );
static const char * BindingFromKey( const char *key );
static bool KeyIsBoundTo( int keyNum, const char *binding );
static void WriteBindings( idFile *f );
};
#endif /* !__KEYINPUT_H__ */