]> git.tdb.fi Git - libs/gui.git/blob - keyboard.cpp
47f2256a20b3eb9f534b84f1b212f3616bad3ba4
[libs/gui.git] / keyboard.cpp
1 #include "keyboard.h"
2 #include <windows.h>
3 #include <msp/graphics/window_private.h>
4 #include "keys.h"
5 #include "keys_private.h"
6
7 #ifndef MAPVK_VK_TO_VSC
8 #define MAPVK_VK_TO_VSC 0
9 #endif
10
11 namespace Msp {
12 namespace Input {
13
14 std::string Keyboard::get_button_name(unsigned btn) const
15 {
16         if(btn==KEY_NONE)
17                 return "None";
18
19         unsigned vkey = key_to_sys(btn);
20         unsigned scan = MapVirtualKey(vkey&0xFF, MAPVK_VK_TO_VSC);
21         scan |= vkey&0x100;
22
23         char buf[128];
24         if(!GetKeyNameText(scan<<16, buf, sizeof(buf)))
25                 return Device::get_button_name(btn);
26
27         return buf;
28 }
29
30 void Keyboard::input_event(const Graphics::Window::Event &event)
31 {
32         switch(event.msg)
33         {
34         case WM_KEYDOWN:
35         case WM_KEYUP:
36         case WM_SYSKEYDOWN:
37         case WM_SYSKEYUP:
38                 {
39                         unsigned key = event.wparam | ((event.lparam>>16)&0x100);
40                         if(key==VK_SHIFT)
41                         {
42                                 if(((event.lparam>>16)&0xFF)==MapVirtualKey(VK_RSHIFT, MAPVK_VK_TO_VSC))
43                                         key = VK_RSHIFT;
44                                 else
45                                         key = VK_LSHIFT;
46                         }
47                         set_button_state(key_from_sys(key), false, true);
48                         if(event.msg==WM_KEYDOWN || event.msg==WM_SYSKEYDOWN)
49                                 set_button_state(key_from_sys(key), true, true);
50                 }
51                 break;
52         case WM_CHAR:
53                 signal_character.emit(event.wparam);
54                 break;
55         }
56 }
57
58 } // namespace Input
59 } // namespace Msp