X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Finput%2Fkeyboard.cpp;h=0f4d4ab848c772efb084f14cd18953c6cb412e26;hb=5ccbbe9ba2daa4d5f0c047be022f6cfeec946598;hp=344048e802b43638df33ba97a4bd0f9059e7e869;hpb=dce7552c5e2f64fcf5f58b0c934bb4a01f6cbcf7;p=libs%2Fgui.git diff --git a/source/input/keyboard.cpp b/source/input/keyboard.cpp index 344048e..0f4d4ab 100644 --- a/source/input/keyboard.cpp +++ b/source/input/keyboard.cpp @@ -2,9 +2,12 @@ #include #else #include +#include #endif -#include -#include +#include +#include +#include +#include #include "keyboard.h" #include "keys.h" @@ -13,15 +16,14 @@ namespace Msp { namespace Input { -Keyboard::Keyboard(Graphics::EventSource &s): - source(s) +Keyboard::Keyboard(Graphics::Window &w): + window(w) { name = "Keyboard"; buttons.resize(N_KEYS_, false); - source.signal_key_press.connect(sigc::mem_fun(this, &Keyboard::key_press)); - source.signal_key_release.connect(sigc::mem_fun(this, &Keyboard::key_release)); + window.signal_input_event.connect(sigc::mem_fun(this, &Keyboard::input_event)); } std::string Keyboard::get_button_name(unsigned btn) const @@ -31,25 +33,51 @@ std::string Keyboard::get_button_name(unsigned btn) const #ifndef WIN32 const char *str = XKeysymToString(key_to_sys(btn)); if(!str) - return format("Key %d", btn); + return Device::get_button_name(btn); return str; #else char buf[128]; unsigned scan = MapVirtualKey(key_to_sys(btn), MAPVK_VK_TO_VSC); if(!GetKeyNameText(scan<<16, buf, sizeof(buf))) - return format("Key %d", btn); + return Device::get_button_name(btn); return buf; #endif } -void Keyboard::key_press(unsigned key, unsigned, unsigned) +void Keyboard::input_event(const Graphics::Window::Event &event) { - set_button_state(key_from_sys(key), true, true); -} - -void Keyboard::key_release(unsigned key, unsigned) -{ - set_button_state(key_from_sys(key), false, true); +#ifdef WIN32 + switch(event.msg) + { + case WM_KEYDOWN: + case WM_KEYUP: + set_button_state(key_from_sys(event.wparam), event.msg==WM_KEYDOWN, true); + break; + case WM_CHAR: + signal_character.emit(event.wparam); + break; + } +#else + switch(event.xevent.type) + { + case KeyPress: + case KeyRelease: + { + KeySym keysym = XKeycodeToKeysym(window.get_display().get_private().display, event.xevent.xkey.keycode, 0); + if(keysym!=NoSymbol) + if(unsigned key = key_from_sys(keysym)) + set_button_state(key, event.xevent.type==KeyPress, true); + if(event.xevent.type==KeyPress) + { + char ch; + if(XLookupString(const_cast(&event.xevent.xkey), &ch, 1, 0, 0)) + // XLookupString always returns Latin-1 + signal_character.emit(static_cast(ch)); + } + } + break; + } +#endif } } // namespace Input