HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
keyboard_key.hpp
1// Copyright Take Vos 2020-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 "keyboard_virtual_key_intf.hpp"
9#include "../utility/utility.hpp"
10#include "../macros.hpp"
11#include <string_view>
12#include <unordered_map>
13#include <format>
14#include <string>
15
16hi_export_module(hikogui.GUI : keyboard_key);
17
18hi_export namespace hi::inline v1 {
19
26 keyboard_modifiers modifiers;
27
35 keyboard_virtual_key virtual_key;
36
37public:
38 constexpr keyboard_key() noexcept : modifiers(keyboard_modifiers::none), virtual_key(keyboard_virtual_key::nul) {}
39
40 constexpr keyboard_key(keyboard_modifiers modifiers, keyboard_virtual_key key) noexcept :
41 modifiers(modifiers), virtual_key(key)
42 {
43 }
44
45 keyboard_key(std::string_view key_combination) : modifiers(keyboard_modifiers::none), virtual_key(keyboard_virtual_key::nul)
46 {
47 auto const modifiers_and_vkey = split(key_combination, '+');
48 hi_assert(modifiers_and_vkey.cbegin() != modifiers_and_vkey.cend());
49
50 auto const end_modifiers = modifiers_and_vkey.cend() - 1;
51 for (auto i = modifiers_and_vkey.cbegin(); i != end_modifiers; ++i) {
52 modifiers |= to_keyboard_modifiers(*i);
53 }
54
55 virtual_key = to_keyboard_virtual_key(modifiers_and_vkey.back());
56 }
57
58 std::size_t hash() const noexcept
59 {
60 return hash_mix(modifiers, virtual_key);
61 }
62
63 [[nodiscard]] friend bool operator==(keyboard_key const &lhs, keyboard_key const &rhs) noexcept
64 {
65 return lhs.modifiers == rhs.modifiers && lhs.virtual_key == rhs.virtual_key;
66 }
67
68 [[nodiscard]] friend std::string to_string(keyboard_key const &rhs) noexcept
69 {
70 return std::format("{}{}", rhs.modifiers, rhs.virtual_key);
71 }
72
73 friend std::ostream &operator<<(std::ostream &lhs, keyboard_key const &rhs)
74 {
75 return lhs << to_string(rhs);
76 }
77};
78
79} // namespace hi::inline v1
80
81template<>
82struct std::hash<hi::keyboard_key> {
83 [[nodiscard]] std::size_t operator()(hi::keyboard_key const &rhs) const noexcept
84 {
85 return rhs.hash();
86 }
87};
The HikoGUI namespace.
Definition array_generic.hpp:20
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
keyboard_modifiers
Key modification keys pressed at the same time as another key.
Definition keyboard_modifiers.hpp:27
keyboard_modifiers to_keyboard_modifiers(std::string_view s)
Parse a key-binding modifier name.
Definition keyboard_modifiers.hpp:60
A key in combination with modifiers.
Definition keyboard_key.hpp:23
T operator()(T... args)