]> git.tdb.fi Git - libs/gui.git/blob - source/input/keyboard.cpp
Drop Id tags and copyright notices from files
[libs/gui.git] / source / input / keyboard.cpp
1 #ifdef WIN32
2 #include <windows.h>
3 #else
4 #include <X11/Xlib.h>
5 #endif
6 #include <msp/strings/formatter.h>
7 #include "../gbase/display.h"
8 #include "keyboard.h"
9 #include "keys.h"
10
11 #define MAPVK_VK_TO_VSC 0
12
13 namespace Msp {
14 namespace Input {
15
16 Keyboard::Keyboard(Graphics::EventSource &s):
17         source(s)
18 {
19         name="Keyboard";
20
21         buttons.resize(N_KEYS_, false);
22
23         source.signal_key_press.connect(sigc::mem_fun(this, &Keyboard::key_press));
24         source.signal_key_release.connect(sigc::mem_fun(this, &Keyboard::key_release));
25 }
26
27 std::string Keyboard::get_button_name(unsigned btn) const
28 {
29         if(btn==0)
30                 return "None";
31 #ifndef WIN32
32         const char *str=XKeysymToString(key_to_sys(btn));
33         if(!str)
34                 return format("Key %d", btn);
35         return str;
36 #else
37         char buf[128];
38         unsigned scan=MapVirtualKey(key_to_sys(btn), MAPVK_VK_TO_VSC);
39         if(!GetKeyNameText(scan<<16, buf, sizeof(buf)))
40                 return format("Key %d", btn);
41         return buf;
42 #endif
43 }
44
45 void Keyboard::key_press(unsigned key, unsigned, unsigned)
46 {
47         set_button_state(key_from_sys(key), true, true);
48 }
49
50 void Keyboard::key_release(unsigned key, unsigned)
51 {
52         set_button_state(key_from_sys(key), false, true);
53 }
54
55 } // namespace Input
56 } // namespace Msp