X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;ds=sidebyside;f=source%2Finput%2Fkeyboard.cpp;h=ea66cf28ab11aa9d74f55680293336f4e5efae83;hb=4c43f46e8fae246416726c9efb71cf5e984d2e08;hp=963a06266fd5a033f91e5e2d448e4968bbbf6c53;hpb=5add89fdd5e5e542ae0e93de2fe9d9b2532c1e07;p=libs%2Fgui.git diff --git a/source/input/keyboard.cpp b/source/input/keyboard.cpp index 963a062..ea66cf2 100644 --- a/source/input/keyboard.cpp +++ b/source/input/keyboard.cpp @@ -1,17 +1,14 @@ -/* $Id$ - -This file is part of libmspgbase -Copyright © 2007-2008, 2010 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ - #ifdef WIN32 #include #else #include +#include #endif -#include -#include "../gbase/display.h" +#include +#include +#include +#include +#include #include "keyboard.h" #include "keys.h" @@ -20,15 +17,14 @@ Distributed under the LGPL namespace Msp { namespace Input { -Keyboard::Keyboard(Graphics::EventSource &s): - source(s) +Keyboard::Keyboard(Graphics::Window &w): + window(w) { - name="Keyboard"; + 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 @@ -36,27 +32,52 @@ std::string Keyboard::get_button_name(unsigned btn) const if(btn==0) return "None"; #ifndef WIN32 - const char *str=XKeysymToString(key_to_sys(btn)); + const char *str = XKeysymToString(key_to_sys(btn)); if(!str) return format("Key %d", btn); return str; #else char buf[128]; - unsigned scan=MapVirtualKey(key_to_sys(btn), MAPVK_VK_TO_VSC); + unsigned scan = MapVirtualKey(key_to_sys(btn), MAPVK_VK_TO_VSC); if(!GetKeyNameText(scan<<16, buf, sizeof(buf))) return format("Key %d", btn); return buf; #endif } -void Keyboard::key_press(unsigned key, unsigned, unsigned) -{ - set_button_state(key_from_sys(key), true, true); -} - -void Keyboard::key_release(unsigned key, unsigned) +void Keyboard::input_event(const Graphics::Window::Event &event) { - 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) + set_button_state(key_from_sys(keysym), 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