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 "../text/grapheme.hpp"
9#include "../required.hpp"
10#include "../assert.hpp"
11#include "../command.hpp"
12#include <utility>
13
14namespace tt {
15
16enum class KeyboardState : uint8_t {
17 Idle = 0x00,
18 CapsLock = 0x01,
19 ScrollLock = 0x02,
20 NumLock = 0x04,
21};
22
23[[nodiscard]] constexpr KeyboardState operator|(KeyboardState lhs, KeyboardState rhs) noexcept {
24 return static_cast<KeyboardState>(static_cast<uint8_t>(lhs) | static_cast<uint8_t>(rhs));
25}
26
27constexpr KeyboardState &operator|=(KeyboardState &lhs, KeyboardState rhs) noexcept {
28 lhs = lhs | rhs;
29 return lhs;
30}
31
32[[nodiscard]] constexpr bool operator>=(KeyboardState lhs, KeyboardState rhs) noexcept {
33 ttlet lhs_ = static_cast<uint8_t>(lhs);
34 ttlet rhs_ = static_cast<uint8_t>(rhs);
35 return (lhs_ & rhs_) == rhs_;
36}
37
38
40 enum class Type : uint8_t {
41 Idle,
43 grapheme,
44 Key,
45 };
46
47 Type type;
48 KeyboardState state;
49
51 keyboard_key key;
52
53 keyboard_event(Type type=Type::Idle) noexcept :
54 type(type), state(), grapheme(), key() {}
55
58 keyboard_event(KeyboardState state, keyboard_modifiers modifiers, keyboard_virtual_key key) noexcept :
59 type(Type::Key), state(state), grapheme(), key(modifiers, key) {}
60
61 keyboard_event(tt::grapheme grapheme, bool full = true) noexcept :
62 type(full ? Type::grapheme : Type::Partialgrapheme), state(), grapheme(std::move(grapheme)), key() {}
63
64 [[nodiscard]] std::vector<command> const &getCommands() const noexcept;
65
66 [[nodiscard]] friend std::string to_string(keyboard_event const &rhs) noexcept {
67 auto r = std::string{"<keyboard_event "};
68
69 switch (rhs.type) {
70 case Type::Idle: r += "Idle"; break;
71 case Type::Partialgrapheme: r += "Partialgrapheme="; break;
72 case Type::grapheme: r += "grapheme="; break;
73 case Type::Key: r += "Key="; break;
74 default: tt_no_default();
75 }
76
77 if (rhs.type == Type::Partialgrapheme || rhs.type == Type::grapheme) {
78 r += to_string(rhs.grapheme);
79 } else if (rhs.type == Type::Key) {
80 r += to_string(rhs.key);
81 }
82
83 r += ">";
84 return r;
85 }
86
87 friend std::ostream &operator<<(std::ostream &lhs, keyboard_event const &rhs) {
88 return lhs << to_string(rhs);
89 }
90};
91
92}
STL namespace.
Definition keyboard_event.hpp:39
Type
Definition keyboard_event.hpp:40
@ Key
Key (+modifiers) was used to send a key.
@ Partialgrapheme
The user is combining a grapheme.
@ grapheme
The user has finished entering a grapheme.
keyboard_event(KeyboardState state, keyboard_modifiers modifiers, keyboard_virtual_key key) noexcept
Create a key-key keyboard event.
Definition keyboard_event.hpp:58
A key in combination with modifiers.
Definition keyboard_key.hpp:20
Definition grapheme.hpp:21
T operator>=(T... args)