HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
keyboard_virtual_key_intf.hpp
1// Copyright Take Vos 2021-2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "keyboard_modifiers.hpp"
8#include "../utility/utility.hpp"
9#include "../macros.hpp"
10#include <unordered_map>
11#include <format>
12#include <string_view>
13#include <string>
14#include <ostream>
15
16hi_export_module(hikogui.GUI : keyboard_virtual_key_intf);
17
18hi_export namespace hi::inline v1 {
19
20enum class keyboard_virtual_key : uint8_t {
21 nul = 0,
22
23 A = 'a',
24 B = 'b',
25 C = 'c',
26 D = 'd',
27 E = 'e',
28 F = 'f',
29 G = 'g',
30 H = 'h',
31 I = 'i',
32 J = 'j',
33 K = 'k',
34 L = 'l',
35 M = 'm',
36 N = 'n',
37 O = 'o',
38 P = 'p',
39 Q = 'q',
40 R = 'r',
41 S = 's',
42 T = 't',
43 U = 'u',
44 V = 'v',
45 W = 'w',
46 X = 'x',
47 Y = 'y',
48 Z = 'z',
49
50 _0 = '0',
51 _1 = '1',
52 _2 = '2',
53 _3 = '3',
54 _4 = '4',
55 _5 = '5',
56 _6 = '6',
57 _7 = '7',
58 _8 = '8',
59 _9 = '9',
60
61 plus = '+',
62 minus = '-',
63 star = '*',
64 slash = '/',
65 percent = '%',
66 tilde = '~',
67 ampersant = '&',
68 pipe = '|',
69 caret = '^',
70 less = '<',
71 equal = '=',
72 greater = '>',
73 open_parentheses = '(',
74 close_parentheses = ')',
75 open_bracket = '[',
76 close_bracket = ']',
77 open_brace = '{',
78 close_brace = '}',
79 period = '.',
80 comma = ',',
81 colon = ':',
82 semi_colon = ';',
83 bang = '!',
84 question = '?',
85 space = ' ',
86 tab = '\t',
87 enter = '\n',
88 backtick = '`',
89 quote = '\'',
90 double_quote = '"',
91 at = '@',
92 hash = '#',
93 dollar = '$',
94 underscore = '_',
95 backslash = '\\',
96
97 F1 = 'A',
98 F2 = 'B',
99 F3 = 'C',
100 F4 = 'D',
101 F5 = 'E',
102 F6 = 'F',
103 F7 = 'G',
104 F8 = 'H',
105 F9 = 'I',
106 F10 = 'J',
107 F11 = 'K',
108 F12 = 'L',
109 F13 = 'M',
110 F14 = 'N',
111 F15 = 'O',
112 F16 = 'P',
113 F17 = 'Q',
114 F18 = 'R',
115 F19 = 'S',
116 F20 = 'T',
117 F21 = 'U',
118 F22 = 'V',
119 F23 = 'W',
120 F24 = 'X',
121
122 home = 0x02, // ASCII start-of-text
123 end = 0x03, // ASCII end-of-text
124 backspace = 0x08, // ASCII backspace
125 clear = 0x0c, // ASCII form-feed
126 insert = 0x1a, // ASCII substitute
127 escape = 0x1b, // ASCII escape
128 _delete = 0x7f, // ASCII delete
129
130 left = 0x80,
131 right,
132 up,
133 down,
134 page_up,
135 page_down,
136
137 menu,
138 print_screen,
139 pause_break,
140 sysmenu,
141
142 media_next_track,
143 media_prev_track,
144 media_stop,
145 media_play_pause,
146
147 volume_mute,
148 volume_up,
149 volume_down,
150
151 browser_back,
152 browser_forward,
153 browser_home,
154 browser_refresh,
155 browser_stop,
156 browser_search,
157 browser_favorites,
158};
159
160// clang-format off
161constexpr auto keyboard_virtual_key_metadata = enum_metadata{
162 keyboard_virtual_key::nul, "nul",
163 keyboard_virtual_key::A, "a",
164 keyboard_virtual_key::B, "b",
165 keyboard_virtual_key::C, "c",
166 keyboard_virtual_key::D, "d",
167 keyboard_virtual_key::E, "e",
168 keyboard_virtual_key::F, "f",
169 keyboard_virtual_key::G, "g",
170 keyboard_virtual_key::H, "h",
171 keyboard_virtual_key::I, "i",
172 keyboard_virtual_key::J, "j",
173 keyboard_virtual_key::K, "k",
174 keyboard_virtual_key::L, "l",
175 keyboard_virtual_key::M, "m",
176 keyboard_virtual_key::N, "n",
177 keyboard_virtual_key::O, "o",
178 keyboard_virtual_key::P, "p",
179 keyboard_virtual_key::Q, "q",
180 keyboard_virtual_key::R, "r",
181 keyboard_virtual_key::S, "s",
182 keyboard_virtual_key::T, "t",
183 keyboard_virtual_key::U, "u",
184 keyboard_virtual_key::V, "v",
185 keyboard_virtual_key::W, "w",
186 keyboard_virtual_key::X, "x",
187 keyboard_virtual_key::Y, "y",
188 keyboard_virtual_key::Z, "z",
189 keyboard_virtual_key::_0, "0",
190 keyboard_virtual_key::_1, "1",
191 keyboard_virtual_key::_2, "2",
192 keyboard_virtual_key::_3, "3",
193 keyboard_virtual_key::_4, "4",
194 keyboard_virtual_key::_5, "5",
195 keyboard_virtual_key::_6, "6",
196 keyboard_virtual_key::_7, "7",
197 keyboard_virtual_key::_8, "8",
198 keyboard_virtual_key::_9, "9",
199 keyboard_virtual_key::plus, "plus",
200 keyboard_virtual_key::minus, "-",
201 keyboard_virtual_key::star, "*",
202 keyboard_virtual_key::slash, "/",
203 keyboard_virtual_key::percent, "%",
204 keyboard_virtual_key::tilde, "~",
205 keyboard_virtual_key::ampersant, "&",
206 keyboard_virtual_key::pipe, "|",
207 keyboard_virtual_key::caret, "^",
208 keyboard_virtual_key::less, "<",
209 keyboard_virtual_key::equal, "=",
210 keyboard_virtual_key::greater, ">",
211 keyboard_virtual_key::open_parentheses, "(",
212 keyboard_virtual_key::close_parentheses, ")",
213 keyboard_virtual_key::open_bracket, "[",
214 keyboard_virtual_key::close_bracket, "]",
215 keyboard_virtual_key::open_brace, "{",
216 keyboard_virtual_key::close_brace, "}",
217 keyboard_virtual_key::period, ".",
218 keyboard_virtual_key::comma, ",",
219 keyboard_virtual_key::colon, ":",
220 keyboard_virtual_key::semi_colon, ";",
221 keyboard_virtual_key::bang, "!",
222 keyboard_virtual_key::question, "?",
223 keyboard_virtual_key::space, "space",
224 keyboard_virtual_key::tab, "tab",
225 keyboard_virtual_key::enter, "enter",
226 keyboard_virtual_key::backtick, "`",
227 keyboard_virtual_key::quote, "quote",
228 keyboard_virtual_key::double_quote, "dquote",
229 keyboard_virtual_key::at, "@",
230 keyboard_virtual_key::hash, "#",
231 keyboard_virtual_key::dollar, "$",
232 keyboard_virtual_key::underscore, "_",
233 keyboard_virtual_key::backslash, "backslash",
234 keyboard_virtual_key::F1, "f1",
235 keyboard_virtual_key::F2, "f2",
236 keyboard_virtual_key::F3, "f3",
237 keyboard_virtual_key::F4, "f4",
238 keyboard_virtual_key::F5, "f5",
239 keyboard_virtual_key::F6, "f6",
240 keyboard_virtual_key::F7, "f7",
241 keyboard_virtual_key::F8, "f8",
242 keyboard_virtual_key::F9, "f9",
243 keyboard_virtual_key::F10, "f10",
244 keyboard_virtual_key::F11, "f11",
245 keyboard_virtual_key::F12, "f12",
246 keyboard_virtual_key::F13, "f13",
247 keyboard_virtual_key::F14, "f14",
248 keyboard_virtual_key::F15, "f15",
249 keyboard_virtual_key::F16, "f16",
250 keyboard_virtual_key::F17, "f17",
251 keyboard_virtual_key::F18, "f18",
252 keyboard_virtual_key::F19, "f19",
253 keyboard_virtual_key::F20, "f20",
254 keyboard_virtual_key::F21, "f21",
255 keyboard_virtual_key::F22, "f22",
256 keyboard_virtual_key::F23, "f23",
257 keyboard_virtual_key::F24, "f24",
258 keyboard_virtual_key::home, "home",
259 keyboard_virtual_key::end, "end",
260 keyboard_virtual_key::backspace, "backspace",
261 keyboard_virtual_key::clear, "clear",
262 keyboard_virtual_key::insert, "insert",
263 keyboard_virtual_key::escape, "escape",
264 keyboard_virtual_key::_delete, "delete",
265 keyboard_virtual_key::left, "left",
266 keyboard_virtual_key::right, "right",
267 keyboard_virtual_key::up, "up",
268 keyboard_virtual_key::down, "down",
269 keyboard_virtual_key::page_up, "page-up",
270 keyboard_virtual_key::page_down, "page-down",
271 keyboard_virtual_key::menu, "menu",
272 keyboard_virtual_key::sysmenu, "sysmenu",
273 keyboard_virtual_key::print_screen, "print-screen",
274 keyboard_virtual_key::pause_break, "pause-break",
275 keyboard_virtual_key::media_next_track, "media-next",
276 keyboard_virtual_key::media_prev_track, "media-prev",
277 keyboard_virtual_key::media_stop, "media-stop",
278 keyboard_virtual_key::media_play_pause, "media-play",
279 keyboard_virtual_key::volume_mute, "volume-mute",
280 keyboard_virtual_key::volume_up, "volume-up",
281 keyboard_virtual_key::volume_down, "volume-down",
282 keyboard_virtual_key::browser_back, "browser-back",
283 keyboard_virtual_key::browser_forward, "browser-forward",
284 keyboard_virtual_key::browser_home, "browser-home",
285 keyboard_virtual_key::browser_refresh, "browser-refresh",
286 keyboard_virtual_key::browser_stop, "browser-stop",
287 keyboard_virtual_key::browser_search, "browser-search",
288 keyboard_virtual_key::browser_favorites, "browser-favorites"
289};
290// clang-format on
291
292constexpr keyboard_virtual_key to_keyboard_virtual_key(std::string_view s)
293{
294 return keyboard_virtual_key_metadata[s];
295}
296
297[[nodiscard]] constexpr keyboard_virtual_key to_keyboard_virtual_key(int key_code, bool extended, keyboard_modifiers modifiers) noexcept;
298
299constexpr std::string_view to_string_view(keyboard_virtual_key key) noexcept
300{
301 return keyboard_virtual_key_metadata[key];
302}
303
304inline std::string to_string(keyboard_virtual_key key) noexcept
305{
306 return std::string{to_string_view(key)};
307}
308
309inline std::ostream& operator<<(std::ostream& lhs, keyboard_virtual_key const& rhs)
310{
311 return lhs << to_string_view(rhs);
312}
313
314} // namespace hi::inline v1
315
316template<>
317struct std::hash<hi::keyboard_virtual_key> {
318 [[nodiscard]] std::size_t operator()(hi::keyboard_virtual_key rhs) const noexcept
319 {
320 return std::hash<uint8_t>{}(static_cast<uint8_t>(rhs));
321 }
322};
323
324// XXX #617 MSVC bug does not handle partial specialization in modules.
325hi_export template<>
326struct std::formatter<hi::keyboard_virtual_key, char> : std::formatter<std::string_view, char> {
327 auto format(hi::keyboard_virtual_key const& t, auto& fc) const
328 {
329 return std::formatter<std::string_view, char>{}.format(hi::to_string_view(t), fc);
330 }
331};
The HikoGUI namespace.
Definition array_generic.hpp:20
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
@ menu
A menu item in a popup overlay Menu item widget accepts keyboard focus from the up/down cursor keys.
T end(T... args)
T left(T... args)
T operator()(T... args)
T to_string(T... args)