openMSX
KeyMappings.hh
Go to the documentation of this file.
1#ifndef KEYMAPPINGS_HH
2#define KEYMAPPINGS_HH
3
4#include "ranges.hh"
5
6#include <imgui.h>
7#include <SDL.h>
8
9#include <array>
10
11namespace openmsx {
12
13// Here we define the mapping between ImGui and SDL (just once).
14// This will be used to generate two mapping functions (for both directions).
16 ImGuiKey imgui;
17 SDL_Keycode sdl;
18};
19inline constexpr auto unsortedKeys = std::to_array<ImGui_SDL_Key>({
20 {ImGuiKey_Tab, SDLK_TAB},
21 {ImGuiKey_LeftArrow, SDLK_LEFT},
22 {ImGuiKey_RightArrow, SDLK_RIGHT},
23 {ImGuiKey_UpArrow, SDLK_UP},
24 {ImGuiKey_DownArrow, SDLK_DOWN},
25 {ImGuiKey_PageUp, SDLK_PAGEUP},
26 {ImGuiKey_PageDown, SDLK_PAGEDOWN},
27 {ImGuiKey_Home, SDLK_HOME},
28 {ImGuiKey_End, SDLK_END},
29 {ImGuiKey_Insert, SDLK_INSERT},
30 {ImGuiKey_Delete, SDLK_DELETE},
31 {ImGuiKey_Backspace, SDLK_BACKSPACE},
32 {ImGuiKey_Space, SDLK_SPACE},
33 {ImGuiKey_Enter, SDLK_RETURN},
34 {ImGuiKey_Escape, SDLK_ESCAPE},
35 {ImGuiKey_Apostrophe, SDLK_QUOTE},
36 {ImGuiKey_Comma, SDLK_COMMA},
37 {ImGuiKey_Minus, SDLK_MINUS},
38 {ImGuiKey_Period, SDLK_PERIOD},
39 {ImGuiKey_Slash, SDLK_SLASH},
40 {ImGuiKey_Semicolon, SDLK_SEMICOLON},
41 {ImGuiKey_Equal, SDLK_EQUALS},
42 {ImGuiKey_LeftBracket, SDLK_LEFTBRACKET},
43 {ImGuiKey_Backslash, SDLK_BACKSLASH},
44 {ImGuiKey_RightBracket, SDLK_RIGHTBRACKET},
45 {ImGuiKey_GraveAccent, SDLK_BACKQUOTE},
46 {ImGuiKey_CapsLock, SDLK_CAPSLOCK},
47 {ImGuiKey_ScrollLock, SDLK_SCROLLLOCK},
48 {ImGuiKey_NumLock, SDLK_NUMLOCKCLEAR},
49 {ImGuiKey_PrintScreen, SDLK_PRINTSCREEN},
50 {ImGuiKey_Pause, SDLK_PAUSE},
51 {ImGuiKey_Keypad0, SDLK_KP_0},
52 {ImGuiKey_Keypad1, SDLK_KP_1},
53 {ImGuiKey_Keypad2, SDLK_KP_2},
54 {ImGuiKey_Keypad3, SDLK_KP_3},
55 {ImGuiKey_Keypad4, SDLK_KP_4},
56 {ImGuiKey_Keypad5, SDLK_KP_5},
57 {ImGuiKey_Keypad6, SDLK_KP_6},
58 {ImGuiKey_Keypad7, SDLK_KP_7},
59 {ImGuiKey_Keypad8, SDLK_KP_8},
60 {ImGuiKey_Keypad9, SDLK_KP_9},
61 {ImGuiKey_KeypadDecimal, SDLK_KP_PERIOD},
62 {ImGuiKey_KeypadDivide, SDLK_KP_DIVIDE},
63 {ImGuiKey_KeypadMultiply, SDLK_KP_MULTIPLY},
64 {ImGuiKey_KeypadSubtract, SDLK_KP_MINUS},
65 {ImGuiKey_KeypadAdd, SDLK_KP_PLUS},
66 {ImGuiKey_KeypadEnter, SDLK_KP_ENTER},
67 {ImGuiKey_KeypadEqual, SDLK_KP_EQUALS},
68 {ImGuiKey_LeftCtrl, SDLK_LCTRL},
69 {ImGuiKey_LeftShift, SDLK_LSHIFT},
70 {ImGuiKey_LeftAlt, SDLK_LALT},
71 {ImGuiKey_LeftSuper, SDLK_LGUI},
72 {ImGuiKey_RightCtrl, SDLK_RCTRL},
73 {ImGuiKey_RightShift, SDLK_RSHIFT},
74 {ImGuiKey_RightAlt, SDLK_RALT},
75 {ImGuiKey_RightSuper, SDLK_RGUI},
76 {ImGuiKey_Menu, SDLK_APPLICATION},
77 {ImGuiKey_0, SDLK_0},
78 {ImGuiKey_1, SDLK_1},
79 {ImGuiKey_2, SDLK_2},
80 {ImGuiKey_3, SDLK_3},
81 {ImGuiKey_4, SDLK_4},
82 {ImGuiKey_5, SDLK_5},
83 {ImGuiKey_6, SDLK_6},
84 {ImGuiKey_7, SDLK_7},
85 {ImGuiKey_8, SDLK_8},
86 {ImGuiKey_9, SDLK_9},
87 {ImGuiKey_A, SDLK_a},
88 {ImGuiKey_B, SDLK_b},
89 {ImGuiKey_C, SDLK_c},
90 {ImGuiKey_D, SDLK_d},
91 {ImGuiKey_E, SDLK_e},
92 {ImGuiKey_F, SDLK_f},
93 {ImGuiKey_G, SDLK_g},
94 {ImGuiKey_H, SDLK_h},
95 {ImGuiKey_I, SDLK_i},
96 {ImGuiKey_J, SDLK_j},
97 {ImGuiKey_K, SDLK_k},
98 {ImGuiKey_L, SDLK_l},
99 {ImGuiKey_M, SDLK_m},
100 {ImGuiKey_N, SDLK_n},
101 {ImGuiKey_O, SDLK_o},
102 {ImGuiKey_P, SDLK_p},
103 {ImGuiKey_Q, SDLK_q},
104 {ImGuiKey_R, SDLK_r},
105 {ImGuiKey_S, SDLK_s},
106 {ImGuiKey_T, SDLK_t},
107 {ImGuiKey_U, SDLK_u},
108 {ImGuiKey_V, SDLK_v},
109 {ImGuiKey_W, SDLK_w},
110 {ImGuiKey_X, SDLK_x},
111 {ImGuiKey_Y, SDLK_y},
112 {ImGuiKey_Z, SDLK_z},
113 {ImGuiKey_F1, SDLK_F1},
114 {ImGuiKey_F2, SDLK_F2},
115 {ImGuiKey_F3, SDLK_F3},
116 {ImGuiKey_F4, SDLK_F4},
117 {ImGuiKey_F5, SDLK_F5},
118 {ImGuiKey_F6, SDLK_F6},
119 {ImGuiKey_F7, SDLK_F7},
120 {ImGuiKey_F8, SDLK_F8},
121 {ImGuiKey_F9, SDLK_F9},
122 {ImGuiKey_F10, SDLK_F10},
123 {ImGuiKey_F11, SDLK_F11},
124 {ImGuiKey_F12, SDLK_F12},
125 {ImGuiKey_F13, SDLK_F13},
126 {ImGuiKey_F14, SDLK_F14},
127 {ImGuiKey_F15, SDLK_F15},
128 {ImGuiKey_F16, SDLK_F16},
129 {ImGuiKey_F17, SDLK_F17},
130 {ImGuiKey_F18, SDLK_F18},
131 {ImGuiKey_F19, SDLK_F19},
132 {ImGuiKey_F20, SDLK_F20},
133 {ImGuiKey_F21, SDLK_F21},
134 {ImGuiKey_F22, SDLK_F22},
135 {ImGuiKey_F23, SDLK_F23},
136 {ImGuiKey_F24, SDLK_F24},
137 {ImGuiKey_AppBack, SDLK_AC_BACK},
138 {ImGuiKey_AppForward, SDLK_AC_FORWARD},
139 // ... order doesn't matter
140});
141
142[[nodiscard]] inline SDL_Keycode ImGuiKey2SDL(ImGuiKey imgui)
143{
144 static constexpr auto lookup = []{
145 auto result = unsortedKeys;
147 return result;
148 }();
149 auto it = ranges::lower_bound(lookup, imgui, {}, &ImGui_SDL_Key::imgui);
150 return ((it != lookup.end()) && (it->imgui == imgui)) ? it->sdl : SDLK_UNKNOWN;
151}
152
153[[nodiscard]] inline ImGuiKey SDLKey2ImGui(SDL_Keycode sdl)
154{
155 static constexpr auto lookup = []{
156 auto result = unsortedKeys;
157 ranges::sort(result, {}, &ImGui_SDL_Key::sdl);
158 return result;
159 }();
160 auto it = ranges::lower_bound(lookup, sdl, {}, &ImGui_SDL_Key::sdl);
161 return ((it != lookup.end()) && (it->sdl == sdl)) ? it->imgui : ImGuiKey_None;
162}
163
164} // namespace openmsx
165
166#endif
const Value * lookup(const hash_map< Key, Value, Hasher, Equal > &map, const Key2 &key)
Definition hash_map.hh:118
This file implemented 3 utility functions:
Definition Autofire.cc:11
constexpr auto unsortedKeys
ImGuiKey SDLKey2ImGui(SDL_Keycode sdl)
SDL_Keycode ImGuiKey2SDL(ImGuiKey imgui)
constexpr void sort(RandomAccessRange &&range)
Definition ranges.hh:51
auto lower_bound(ForwardRange &&range, const T &value, Compare comp={}, Proj proj={})
Definition ranges.hh:117