HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
keyboard_event.hpp
1// Copyright Take Vos 2020-2021.
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_key.hpp"
8#include "../unicode/grapheme.hpp"
9#include "../geometry/transform.hpp"
10#include "../required.hpp"
11#include "../assert.hpp"
12#include "../command.hpp"
13#include "../cast.hpp"
14#include <utility>
15
16namespace hi::inline v1 {
17
18enum class KeyboardState : uint8_t {
19 Idle = 0x00,
20 CapsLock = 0x01,
21 ScrollLock = 0x02,
22 NumLock = 0x04,
23};
24
25[[nodiscard]] constexpr KeyboardState operator|(KeyboardState const& lhs, KeyboardState const& rhs) noexcept
26{
27 return static_cast<KeyboardState>(to_underlying(lhs) | to_underlying(rhs));
28}
29
30[[nodiscard]] constexpr KeyboardState operator&(KeyboardState const& lhs, KeyboardState const& rhs) noexcept
31{
32 return static_cast<KeyboardState>(to_underlying(lhs) & to_underlying(rhs));
33}
34
35constexpr KeyboardState& operator|=(KeyboardState& lhs, KeyboardState const& rhs) noexcept
36{
37 return lhs = lhs | rhs;
38}
39
40bool operator>=(KeyboardState const& lhs, KeyboardState const& rhs) = delete;
41
42[[nodiscard]] constexpr bool any(KeyboardState const& rhs) noexcept
43{
44 return static_cast<bool>(to_underlying(rhs));
45}
46
48 enum class Type : uint8_t {
49 Idle,
50 partial_grapheme,
51 grapheme,
52 Key,
53 };
54
55 Type type;
56 KeyboardState state;
57
58 hi::grapheme grapheme;
59 keyboard_key key;
60
61 keyboard_event(Type type = Type::Idle) noexcept : type(type), state(), grapheme(), key() {}
62
65 keyboard_event(KeyboardState state, keyboard_modifiers modifiers, keyboard_virtual_key key) noexcept :
66 type(Type::Key), state(state), grapheme(), key(modifiers, key)
67 {
68 }
69
70 keyboard_event(hi::grapheme grapheme, bool full = true) noexcept :
71 type(full ? Type::grapheme : Type::partial_grapheme), state(), grapheme(std::move(grapheme)), key()
72 {
73 }
74
75 [[nodiscard]] std::vector<command> const& getCommands() const noexcept;
76
77 [[nodiscard]] friend std::string to_string(keyboard_event const& rhs) noexcept
78 {
79 auto r = std::string{"<keyboard_event "};
80
81 switch (rhs.type) {
82 case Type::Idle: r += "Idle"; break;
83 case Type::partial_grapheme: r += "partial_grapheme="; break;
84 case Type::grapheme: r += "grapheme="; break;
85 case Type::Key: r += "Key="; break;
86 default: hi_no_default();
87 }
88
89 if (rhs.type == Type::partial_grapheme || rhs.type == Type::grapheme) {
90 r += to_string(rhs.grapheme);
91 } else if (rhs.type == Type::Key) {
92 r += to_string(rhs.key);
93 }
94
95 r += ">";
96 return r;
97 }
98
99 friend std::ostream& operator<<(std::ostream& lhs, keyboard_event const& rhs)
100 {
101 return lhs << to_string(rhs);
102 }
103};
104
105} // namespace hi::inline v1
This file includes required definitions.
constexpr alignment operator|(horizontal_alignment lhs, vertical_alignment rhs) noexcept
Combine vertical and horizontal alignment.
Definition alignment.hpp:200
STL namespace.
Definition keyboard_event.hpp:47
Type
Definition keyboard_event.hpp:48
keyboard_event(KeyboardState state, keyboard_modifiers modifiers, keyboard_virtual_key key) noexcept
Create a key-key keyboard event.
Definition keyboard_event.hpp:65
A key in combination with modifiers.
Definition keyboard_key.hpp:20
A grapheme, what a user thinks a character is.
Definition grapheme.hpp:34
T operator>=(T... args)
T to_string(T... args)