]> git.tdb.fi Git - libs/gui.git/blob - source/input/keyboard.cpp
Hide the platform-specific parts of other classes as well
[libs/gui.git] / source / input / keyboard.cpp
1 /* $Id$
2
3 This file is part of libmspgbase
4 Copyright © 2007-2008  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #ifdef WIN32
9 #include <windows.h>
10 #else
11 #include <X11/Xlib.h>
12 #endif
13 #include <msp/strings/formatter.h>
14 #include "../gbase/display.h"
15 #include "keyboard.h"
16 #include "keys.h"
17
18 #define MAPVK_VK_TO_VSC 0
19
20 namespace Msp {
21 namespace Input {
22
23 Keyboard::Keyboard(Graphics::Window &w):
24         window(w)
25 {
26         name="Keyboard";
27
28         buttons.resize(N_KEYS_, false);
29
30         window.signal_key_press.connect(sigc::mem_fun(this, &Keyboard::key_press));
31         window.signal_key_release.connect(sigc::mem_fun(this, &Keyboard::key_release));
32 }
33
34 std::string Keyboard::get_button_name(unsigned btn) const
35 {
36         if(btn==0)
37                 return "None";
38 #ifndef WIN32
39         const char *str=XKeysymToString(key_to_sys(btn));
40         if(!str)
41                 return format("Key %d", btn);
42         return str;
43 #else
44         char buf[128];
45         unsigned scan=MapVirtualKey(key_to_sys(btn), MAPVK_VK_TO_VSC);
46         if(!GetKeyNameText(scan<<16, buf, sizeof(buf)))
47                 return format("Key %d", btn);
48         return buf;
49 #endif
50 }
51
52 void Keyboard::key_press(unsigned key, unsigned, unsigned)
53 {
54         set_button_state(key_from_sys(key), true, true);
55 }
56
57 void Keyboard::key_release(unsigned key, unsigned)
58 {
59         set_button_state(key_from_sys(key), false, true);
60 }
61
62 } // namespace Input
63 } // namespace Msp