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