From 82c06412ef39522c4deab457ce7a3e78160d8a19 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Fri, 22 Feb 2008 22:36:23 +0000 Subject: keypad and numlock support --- src/client/console.cc | 16 ++--- src/client/console.h | 4 +- src/client/input.cc | 4 +- src/client/keyboard.cc | 164 +++++++++++++++++++++++++++++++++++++++---------- src/client/keyboard.h | 4 +- 5 files changed, 142 insertions(+), 50 deletions(-) diff --git a/src/client/console.cc b/src/client/console.cc index 9c7c6ca..85cb72e 100644 --- a/src/client/console.cc +++ b/src/client/console.cc @@ -204,11 +204,11 @@ void toggle() } } -void keypressed(const SDL_keysym &keysym) +void keypressed(int key) { std::deque::reverse_iterator upit; - switch( keysym.sym ) { + switch( key ) { case SDLK_TAB: core::CommandBuffer::complete( (*history_pos), input_pos); break; @@ -227,7 +227,6 @@ void keypressed(const SDL_keysym &keysym) input_pos = 0; } break; - case SDLK_KP8: case SDLK_UP: upit = history_pos; ++upit; @@ -236,27 +235,22 @@ void keypressed(const SDL_keysym &keysym) input_pos = (*history_pos).size(); } break; - case SDLK_KP2: case SDLK_DOWN: if (history_pos != history.rbegin()) { --history_pos; input_pos = (*history_pos).size(); } break; - case SDLK_KP7: case SDLK_HOME: input_pos = 0; break; - case SDLK_KP1: case SDLK_END: input_pos = (*history_pos).size(); break; - case SDLK_KP4: case SDLK_LEFT: if (input_pos > 0) input_pos--; break; - case SDLK_KP6: case SDLK_RIGHT: if (input_pos < (*history_pos).size()) input_pos++; @@ -276,11 +270,11 @@ void keypressed(const SDL_keysym &keysym) else console_scroll = 0; break; default: - if ((keysym.sym >= 32 ) && (keysym.sym <175)) { + if ((key >= 32 ) && (key <175)) { if (input_pos == (*history_pos).size()) - (*history_pos) += keysym_to_char(keysym); + (*history_pos) += (char)key; else - (*history_pos).insert(input_pos, 1, keysym_to_char(keysym)); + (*history_pos).insert(input_pos, 1, (char)key); input_pos++; } break; diff --git a/src/client/console.h b/src/client/console.h index e96c0d4..d9e78a0 100644 --- a/src/client/console.h +++ b/src/client/console.h @@ -9,8 +9,6 @@ #include "sys/consoleinterface.h" -#include - #include #include @@ -42,7 +40,7 @@ void draw(); void toggle(); /// handle keyboard input -void keypressed(const SDL_keysym &keysym); +void keypressed(int key); /// true of the console is visible bool visible(); diff --git a/src/client/input.cc b/src/client/input.cc index 5dda3fb..c9c2514 100644 --- a/src/client/input.cc +++ b/src/client/input.cc @@ -28,11 +28,13 @@ void init() { con_print << "Initializing input..." << std::endl; client::setkeyboardmode(console::visible()); +// SDL_EnableUNICODE(1); } void shutdown() { con_print << "Shutting down input..." << std::endl; +// SDL_EnableUNICODE(0); } // handle key release for the game world @@ -115,7 +117,7 @@ void frame(float seconds) console::toggle(); } else if (console::visible()) { // send key events to the console - console::keypressed(event.key.keysym); + console::keypressed(translate_keysym(event.key.keysym)); } else if (core::application()->connected() && core::localcontrol()) { // send key events to the game world keypressed(event.key.keysym); diff --git a/src/client/keyboard.cc b/src/client/keyboard.cc index 45e9284..8566ded 100644 --- a/src/client/keyboard.cc +++ b/src/client/keyboard.cc @@ -18,95 +18,193 @@ void setkeyboardmode(bool input) SDL_EnableKeyRepeat(10, SDL_DEFAULT_REPEAT_INTERVAL); } -char keysym_to_char(const SDL_keysym &keysym) +int translate_keysym(const SDL_keysym &keysym) { - char c = (char) keysym.sym; - bool shiftstate = false; + + int key = keysym.sym; + bool shift = false; + bool numlock = false; + + + // keypad keys + if (keysym.mod & KMOD_NUM) { + numlock = true; + switch(key) { + case SDLK_KP0: + return '0'; + break; + case SDLK_KP1: + return '1'; + break; + case SDLK_KP2: + return '2'; + break; + case SDLK_KP3: + return '3'; + break; + case SDLK_KP4: + return '4'; + break; + case SDLK_KP5: + return '5'; + break; + case SDLK_KP6: + return '6'; + break; + case SDLK_KP7: + return '7'; + break; + case SDLK_KP8: + return '8'; + break; + case SDLK_KP9: + return '9'; + break; + } + } else { + switch(key) { + case SDLK_KP0: + return SDLK_INSERT; + break; + case SDLK_KP1: + return SDLK_END; + break; + case SDLK_KP2: + return SDLK_DOWN; + break; + case SDLK_KP3: + return SDLK_PAGEDOWN; + break; + case SDLK_KP4: + return SDLK_LEFT; + break; + case SDLK_KP6: + return SDLK_RIGHT; + break; + case SDLK_KP7: + return SDLK_HOME; + break; + case SDLK_KP8: + return SDLK_UP; + break; + case SDLK_KP9: + return SDLK_PAGEUP; + break; + } + } + + // special keys + switch(key) { + case SDLK_KP_ENTER: + return SDLK_RETURN; + break; + case SDLK_KP_PERIOD: + return '.'; + break; + case SDLK_KP_DIVIDE: + return '/'; + break; + case SDLK_KP_MULTIPLY: + return '*'; + break; + case SDLK_KP_MINUS: + return '-'; + break; + case SDLK_KP_PLUS: + return '+'; + break; + case SDLK_KP_EQUALS: + return '='; + break; + } + + // caps lock if (keysym.mod & KMOD_CAPS) - shiftstate = true; + shift = true; + // left/right shift if ((keysym.mod & KMOD_LSHIFT) || (keysym.mod & KMOD_RSHIFT)) { - shiftstate = !shiftstate; + shift = !shift; } - if (!shiftstate) - return c; + if (shift) { + if ((key >= 'a' && key <= 'z')) { + key = key + 'A' - 'a'; + return key; + } - if ((c >= 'a' && c <= 'z')) { - c = c + 'A' - 'a'; - } else { - switch (c) { + switch (key) { case '`': - c = '~'; + key = '~'; break; case '1': - c = '!'; + key = '!'; break; case '2': - c = '@'; + key = '@'; break; case '3': - c = '#'; + key = '#'; break; case '4': - c = '$'; + key = '$'; break; case '5': - c = '%'; + key = '%'; break; case '6': - c = '^'; + key = '^'; break; case '7': - c = '&'; + key = '&'; break; case '8': - c = '*'; + key = '*'; break; case '9': - c = '('; + key = '('; break; case '0': - c = ')'; + key = ')'; break; case '-': - c = '_'; + key = '_'; break; case '=': - c = '+'; + key = '+'; break; // second row case '[': - c = '{'; + key = '{'; break; case ']': - c = '}'; + key = '}'; break; case '|': - c = '\\'; + key = '\\'; break; // third row case ';': - c = ':'; + key = ':'; break; case '\'': - c = '"'; + key = '"'; break; // fourth row case ',': - c = '<'; + key = '<'; break; case '.': - c = '>'; + key = '>'; break; case '/': - c = '?'; + key = '?'; break; } } - return c; + return key; } } // namespace client diff --git a/src/client/keyboard.h b/src/client/keyboard.h index 10953c9..6805818 100644 --- a/src/client/keyboard.h +++ b/src/client/keyboard.h @@ -11,8 +11,8 @@ namespace client { -/// convert SDL_keysym to an ascii character -char keysym_to_char(const SDL_keysym &keysym); +/// convert SDL_keysym to a keystroke +int translate_keysym(const SDL_keysym &keysym); /// set the keyboard input mode /** @param input true for console input, false for game input -- cgit v1.2.3