HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
KeyboardEvent.hpp
1// Copyright 2020 Pokitec
2// All rights reserved.
3
4#pragma once
5
6#include "TTauri/GUI/KeyboardKey.hpp"
7#include "TTauri/Text/Grapheme.hpp"
8#include "TTauri/Foundation/required.hpp"
9#include "TTauri/Foundation/assert.hpp"
10#include <utility>
11
12namespace tt {
13
14enum class KeyboardState : uint8_t {
15 Idle = 0x00,
16 CapsLock = 0x01,
17 ScrollLock = 0x02,
18 NumLock = 0x04,
19};
20
21[[nodiscard]] constexpr KeyboardState operator|(KeyboardState lhs, KeyboardState rhs) noexcept {
22 return static_cast<KeyboardState>(static_cast<uint8_t>(lhs) | static_cast<uint8_t>(rhs));
23}
24
25constexpr KeyboardState &operator|=(KeyboardState &lhs, KeyboardState rhs) noexcept {
26 lhs = lhs | rhs;
27 return lhs;
28}
29
30[[nodiscard]] constexpr bool operator>=(KeyboardState lhs, KeyboardState rhs) noexcept {
31 ttlet lhs_ = static_cast<uint8_t>(lhs);
32 ttlet rhs_ = static_cast<uint8_t>(rhs);
33 return (lhs_ & rhs_) == rhs_;
34}
35
36
38 enum class Type : uint8_t {
39 Idle,
40 Entered,
41 Exited,
43 Grapheme,
44 Key,
45 };
46
47 Type type;
48 KeyboardState state;
49
50 Grapheme grapheme;
51 KeyboardKey key;
52
53 KeyboardEvent(Type type=Type::Idle) noexcept :
54 type(type), state(), grapheme(), key() {}
55
58 KeyboardEvent(KeyboardState state, KeyboardModifiers modifiers, KeyboardVirtualKey key) noexcept :
59 type(Type::Key), state(state), grapheme(), key(modifiers, key) {}
60
61 KeyboardEvent(Grapheme grapheme, bool full=true) noexcept :
62 type(full ? Type::Grapheme : Type::PartialGrapheme), state(), grapheme(std::move(grapheme)), key() {}
63
64 [[nodiscard]] static KeyboardEvent entered() noexcept {
65 return KeyboardEvent(Type::Entered);
66 }
67
68 [[nodiscard]] static KeyboardEvent exited() noexcept {
69 return KeyboardEvent(Type::Exited);
70 }
71
72 [[nodiscard]] std::vector<string_ltag> const &getCommands() const noexcept;
73
74 [[nodiscard]] friend std::string to_string(KeyboardEvent const &rhs) noexcept {
75 auto r = std::string{"<KeyboardEvent "};
76
77 switch (rhs.type) {
78 case Type::Idle: r += "Idle"; break;
79 case Type::Entered: r += "Entered"; break;
80 case Type::Exited: r += "Exited"; break;
81 case Type::PartialGrapheme: r += "PartialGrapheme="; break;
82 case Type::Grapheme: r += "Grapheme="; break;
83 case Type::Key: r += "Key="; break;
84 default: tt_no_default;
85 }
86
87 if (rhs.type == Type::PartialGrapheme || rhs.type == Type::Grapheme) {
88 r += to_string(rhs.grapheme);
89 } else if (rhs.type == Type::Key) {
90 r += to_string(rhs.key);
91 }
92
93 r += ">";
94 return r;
95 }
96
97 friend std::ostream &operator<<(std::ostream &lhs, KeyboardEvent const &rhs) {
98 return lhs << to_string(rhs);
99 }
100};
101
102}
STL namespace.
Definition KeyboardEvent.hpp:37
Type
Definition KeyboardEvent.hpp:38
@ Grapheme
The user has finished entering a grapheme.
@ Exited
Keyboard focus was taken away.
@ Key
Key (+modifiers) was used to send a key.
@ Entered
Keyboard focus was given.
@ PartialGrapheme
The user is combining a grapheme.
KeyboardEvent(KeyboardState state, KeyboardModifiers modifiers, KeyboardVirtualKey key) noexcept
Create a key-key keyboard event.
Definition KeyboardEvent.hpp:58
A key in combination with modifiers.
Definition KeyboardKey.hpp:23
Definition Grapheme.hpp:20
T operator>=(T... args)