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