From 7a4a508629d370c4a79791c7e62fd6f59e8564e9 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Mon, 10 Mar 2008 15:18:28 +0000 Subject: [PATCH] Add platform independent constants for keys (win32 code untested) --- source/gbase/window.cpp | 10 +-- source/input/keyboard.cpp | 18 +++-- source/input/keyboard.h | 4 + source/input/keys.cpp | 138 +++++++++++++++++++++++++++++++++ source/input/keys.h | 155 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 313 insertions(+), 12 deletions(-) create mode 100644 source/input/keys.cpp create mode 100644 source/input/keys.h diff --git a/source/gbase/window.cpp b/source/gbase/window.cpp index da877e9..4a14441 100644 --- a/source/gbase/window.cpp +++ b/source/gbase/window.cpp @@ -19,8 +19,6 @@ Distributed under the LGPL using namespace std; -#include - namespace Msp { namespace Graphics { @@ -231,11 +229,11 @@ void Window::event(const XEvent &ev) char buf[16]; XLookupString(const_cast(&ev.xkey), buf, sizeof(buf), 0, 0); // XXX Handle the result according to locale - signal_key_press.emit(ev.xkey.keycode, ev.xkey.state, buf[0]); + signal_key_press.emit(XKeycodeToKeysym(display.get_display(), ev.xkey.keycode, 0), ev.xkey.state, buf[0]); } break; case KeyRelease: - signal_key_release.emit(ev.xkey.keycode, ev.xkey.state); + signal_key_release.emit(XKeycodeToKeysym(display.get_display(), ev.xkey.keycode, 0), ev.xkey.state); break; case ConfigureNotify: options.width=ev.xconfigure.width; @@ -264,10 +262,10 @@ int Window::wndproc(UINT msg, WPARAM wp, LPARAM lp) switch(msg) { case WM_KEYDOWN: - signal_key_press.emit((lp>>16)&0x1FF, 0, wp); + signal_key_press.emit(wp, 0, wp); break; case WM_KEYUP: - signal_key_release.emit((lp>>16)&0x1FF, 0); + signal_key_release.emit(wp, 0); break; case WM_LBUTTONDOWN: signal_button_press.emit(GET_X_LPARAM(lp), GET_Y_LPARAM(lp), 1, 0); diff --git a/source/input/keyboard.cpp b/source/input/keyboard.cpp index 240d700..4372be4 100644 --- a/source/input/keyboard.cpp +++ b/source/input/keyboard.cpp @@ -8,6 +8,7 @@ Distributed under the LGPL #include #include "../gbase/display.h" #include "keyboard.h" +#include "keys.h" namespace Msp { namespace Input { @@ -17,7 +18,7 @@ Keyboard::Keyboard(Graphics::Window &w): { name="Keyboard"; - buttons.resize(256, false); + buttons.resize(N_KEYS_, false); window.signal_key_press.connect(sigc::mem_fun(this, &Keyboard::key_press)); window.signal_key_release.connect(sigc::mem_fun(this, &Keyboard::key_release)); @@ -25,12 +26,17 @@ Keyboard::Keyboard(Graphics::Window &w): std::string Keyboard::get_button_name(unsigned btn) const { + if(btn==0) + return "None"; #ifndef WIN32 - KeySym ksym=XKeycodeToKeysym(window.get_display().get_display(), btn, 0); - return XKeysymToString(ksym); + const char *str=XKeysymToString(key_to_sys(btn)); + if(!str) + return format("Key %d", btn); + return str; #else char buf[128]; - if(!GetKeyNameText(btn<<16, buf, sizeof(buf))) + unsigned scan=MapVirtualKey(key_to_sys(btn), MAPVK_VK_TO_VCS); + if(!GetKeyNameText(scan<<16, buf, sizeof(buf))) return format("Key %d", btn); return buf; #endif @@ -38,12 +44,12 @@ std::string Keyboard::get_button_name(unsigned btn) const void Keyboard::key_press(unsigned key, unsigned, unsigned) { - set_button_state(key, true, true); + set_button_state(key_from_sys(key), true, true); } void Keyboard::key_release(unsigned key, unsigned) { - set_button_state(key, false, true); + set_button_state(key_from_sys(key), false, true); } } // namespace Input diff --git a/source/input/keyboard.h b/source/input/keyboard.h index 54479ea..1b70fec 100644 --- a/source/input/keyboard.h +++ b/source/input/keyboard.h @@ -14,6 +14,10 @@ Distributed under the LGPL namespace Msp { namespace Input { +/** +Adapts key events from a window to the abstracted input framework. Key codes +are translated to platform-independent values. See keys.h for a list. +*/ class Keyboard: public Device { private: diff --git a/source/input/keys.cpp b/source/input/keys.cpp new file mode 100644 index 0000000..d7de3b1 --- /dev/null +++ b/source/input/keys.cpp @@ -0,0 +1,138 @@ +/* $Id$ + +This file is part of libmspgbase +Copyright © 2007 Mikko Rasa, Mikkosoft Productions +Distributed under the LGPL +*/ + +#include +#ifndef WIN32 +#include +#endif +#include +#include "keys.h" + +using namespace std; + +namespace { + +unsigned keymap[Msp::Input::N_KEYS_]= +{ +#ifndef WIN32 + 0, 0, 0, 0, 0, 0, 0, 0, + XK_BackSpace, XK_Tab, XK_Return, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, XK_Escape, 0, 0, 0, 0, + + XK_space, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, XK_plus, XK_comma, XK_minus, XK_period, XK_slash, + XK_0, XK_1, XK_2, XK_3, XK_4, XK_5, XK_6, XK_7, + XK_8, XK_9, 0, XK_semicolon, XK_less, XK_equal, 0, 0, + + 0, XK_a, XK_b, XK_c, XK_d, XK_e, XK_f, XK_g, + XK_h, XK_i, XK_j, XK_k, XK_l, XK_m, XK_n, XK_o, + XK_p, XK_q, XK_r, XK_s, XK_t, XK_u, XK_v, XK_w, + XK_x, XK_y, XK_z, XK_bracketleft, XK_backslash, XK_bracketright, 0, 0, + + XK_grave, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + + XK_adiaeresis, XK_odiaeresis, XK_udiaeresis, XK_aring, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + + XK_Left, XK_Right, XK_Up, XK_Down, 0, 0, 0, 0, + XK_Home, XK_End, XK_Page_Up, XK_Page_Down, XK_Insert, XK_Delete, 0, 0, + XK_F1, XK_F2, XK_F3, XK_F4, XK_F5, XK_F6, XK_F7, XK_F8, + XK_F9, XK_F10, XK_F11, XK_F12, 0, 0, 0, 0, + + XK_Shift_L, XK_Shift_R, XK_Control_L, XK_Control_R, XK_Alt_L, XK_Alt_R, XK_Super_L, XK_Super_R, + XK_Caps_Lock, XK_Scroll_Lock, XK_Num_Lock, 0, 0, 0, 0, 0, + XK_KP_0, XK_KP_1, XK_KP_2, XK_KP_3, XK_KP_4, XK_KP_5, XK_KP_6, XK_KP_7, + XK_KP_8, XK_KP_9, XK_KP_Add, XK_KP_Subtract, XK_KP_Multiply, XK_KP_Divide, XK_KP_Separator, 0, + + XK_Pause, XK_Print, XK_Menu, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, +#else + 0, 0, 0, 0, 0, 0, 0, 0, + VK_BACK, VK_TAB, VK_RETURN, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, VK_ESCAPE, 0, 0, 0, 0, + + VK_SPACE, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + '0', '1', '2', '3', '4', '5', '6', '7', + '8', '9', 0, 0, 0, 0, 0, 0, + + 0, 'A', 'B', 'C', 'D', 'E', 'F', 'G', + 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', + 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', + 'X', 'Y', 'Z', 0, 0, 0, 0, 0, + + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + + VK_LEFT, VK_RIGHT, VK_UP, VK_DOWN, 0, 0, 0, 0, + VK_HOME, VK_END, VK_PRIOR, VK_NEXT, VK_INSERT, VK_DELETE, 0, 0, + 0, VK_F1, VK_F2, VK_F3, VK_F4, VK_F5, VK_F6, VK_F7, + VK_F8, VK_F9, VK_F10, VK_F11, VK_F12, 0, 0, 0, + + VK_LSHIFT, VK_RSHIFT, VK_LCONTROL, VK_RCONTROL, 0, 0, VK_LWIN, VK_RWIN, + VK_CAPITAL, VK_SCROLL, VK_NUMLOCK, 0, 0, 0, 0, 0, + VK_NUMPAD0, VK_NUMPAD1, VK_NUMPAD2, VK_NUMPAD3, VK_NUMPAD4, VK_NUMPAD5, VK_NUMPAD6, VK_NUMPAD7, + VK_NUMPAD8, VK_NUMPAD9, VK_ADD, VK_SUBTRACT, VK_MULTIPLY, VK_DIVIDE, VK_SEPARATOR, 0, + + VK_PAUSE, VK_SNAPSHOT, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, +#endif +}; + +} + +namespace Msp { +namespace Input { + +unsigned key_from_sys(unsigned code) +{ + static bool init_done=false; + static map reverse_map; + + if(!init_done) + { + for(unsigned i=0; i::const_iterator i=reverse_map.find(code); + if(i!=reverse_map.end()) + return i->second; + + return 0; +} + +unsigned key_to_sys(unsigned key) +{ + if(key>N_KEYS_) + throw InvalidParameterValue("Key out of range"); + return keymap[key]; +} + +} // namespace Input +} // namespace Msp diff --git a/source/input/keys.h b/source/input/keys.h new file mode 100644 index 0000000..e17459a --- /dev/null +++ b/source/input/keys.h @@ -0,0 +1,155 @@ +/* $Id$ + +This file is part of libmspgbase +Copyright © 2007 Mikko Rasa, Mikkosoft Productions +Distributed under the LGPL +*/ + +#ifndef MSP_INPUT_KEYS_H_ +#define MSP_INPUT_KEYS_H_ + +namespace Msp { +namespace Input { + +enum +{ + // Codes 0x01-0x7E reserved for ASCII + KEY_BACKSPACE = 0x08, + KEY_TAB = 0x09, + KEY_ENTER = 0x0A, + KEY_ESC = 0x1B, + KEY_SPACE = 0x20, + + KEY_APOSTROPHE = 0x27, + KEY_PLUS = 0x2B, + KEY_COMMA = 0x2C, + KEY_MINUS = 0x2D, + KEY_PERIOD = 0x2E, + KEY_SLASH = 0x2F, + + KEY_0 = 0x30, + KEY_1, + KEY_2, + KEY_3, + KEY_4, + KEY_5, + KEY_6, + KEY_7, + KEY_8, + KEY_9, + + KEY_SEMICOLON = 0x3B, + KEY_LESS = 0x3C, + KEY_EQUAL = 0x3D, + + KEY_A = 0x41, + KEY_B, + KEY_C, + KEY_D, + KEY_E, + KEY_F, + KEY_G, + KEY_H, + KEY_I, + KEY_J, + KEY_K, + KEY_L, + KEY_M, + KEY_N, + KEY_O, + KEY_P, + KEY_Q, + KEY_R, + KEY_S, + KEY_T, + KEY_U, + KEY_V, + KEY_W, + KEY_X, + KEY_Y, + KEY_Z, + + KEY_BRACKET_L = 0x5B, + KEY_BACKSLASH = 0x5C, + KEY_BRACKET_R = 0x5D, + KEY_GRAVE = 0x60, + + // Various non-ASCII characters + KEY_ADIAERESIS = 0x80, + KEY_ODIAERESIS, + KEY_UDIAERESIS, + KEY_ARING, + + // Arrow keys + KEY_LEFT = 0xA0, + KEY_RIGHT, + KEY_UP, + KEY_DOWN, + + // The block above arrow keys + KEY_HOME = 0xA8, + KEY_END, + KEY_PGUP, + KEY_PGDN, + KEY_INSERT, + KEY_DELETE, + + // Function keys + KEY_F1 = 0xB1, + KEY_F2, + KEY_F3, + KEY_F4, + KEY_F5, + KEY_F6, + KEY_F7, + KEY_F8, + KEY_F9, + KEY_F10, + KEY_F11, + KEY_F12, + + // Modifier keys + KEY_SHIFT_L = 0xC0, + KEY_SHIFT_R, + KEY_CTRL_L, + KEY_CTRL_R, + KEY_ALT_L, + KEY_ALT_R, + KEY_SUPER_L, + KEY_SUPER_R, + KEY_CAPS_LOCK, + KEY_SCROLL_LOCK, + KEY_NUM_LOCK, + + // Keypad + KEY_KP0 = 0xD0, + KEY_KP1, + KEY_KP2, + KEY_KP3, + KEY_KP4, + KEY_KP5, + KEY_KP6, + KEY_KP7, + KEY_KP8, + KEY_KP9, + KEY_KP_ADD, + KEY_KP_SUBTRACT, + KEY_KP_MULTIPLY, + KEY_KP_DIVIDE, + KEY_KP_SEPARATOR, + + // Miscellaneous keys + KEY_PAUSE = 0xE0, + KEY_PRINT_SCREEN, + KEY_MENU, + + N_KEYS_ = 0x100 +}; + +extern unsigned key_from_sys(unsigned); +extern unsigned key_to_sys(unsigned); + +} // namespace Input +} // namespace Msp + +#endif -- 2.43.0