]> git.tdb.fi Git - libs/gui.git/blobdiff - source/input/keyboard.cpp
Add EventSource abstraction layer below Window
[libs/gui.git] / source / input / keyboard.cpp
index 240d700fccc4b45620cbd40c4f728face25f7c58..963a06266fd5a033f91e5e2d448e4968bbbf6c53 100644 (file)
@@ -1,36 +1,49 @@
 /* $Id$
 
 This file is part of libmspgbase
-Copyright © 2007 Mikko Rasa, Mikkosoft Productions
+Copyright © 2007-2008, 2010  Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
 
+#ifdef WIN32
+#include <windows.h>
+#else
+#include <X11/Xlib.h>
+#endif
 #include <msp/strings/formatter.h>
 #include "../gbase/display.h"
 #include "keyboard.h"
+#include "keys.h"
+
+#define MAPVK_VK_TO_VSC 0
 
 namespace Msp {
 namespace Input {
 
-Keyboard::Keyboard(Graphics::Window &w):
-       window(w)
+Keyboard::Keyboard(Graphics::EventSource &s):
+       source(s)
 {
        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));
+       source.signal_key_press.connect(sigc::mem_fun(this, &Keyboard::key_press));
+       source.signal_key_release.connect(sigc::mem_fun(this, &Keyboard::key_release));
 }
 
 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_VSC);
+       if(!GetKeyNameText(scan<<16, buf, sizeof(buf)))
                return format("Key %d", btn);
        return buf;
 #endif
@@ -38,12 +51,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