-
Notifications
You must be signed in to change notification settings - Fork 715
/
keys.hh
133 lines (109 loc) · 3.13 KB
/
keys.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#ifndef keys_hh_INCLUDED
#define keys_hh_INCLUDED
#include "coord.hh"
#include "flags.hh"
#include "hash.hh"
#include "meta.hh"
#include "optional.hh"
#include "unicode.hh"
#include "vector.hh"
#include <cstdint>
namespace Kakoune
{
struct Key
{
enum class MouseButton
{
Left,
Middle,
Right
};
enum class Modifiers : int
{
None = 0,
Control = 1 << 0,
Alt = 1 << 1,
Shift = 1 << 2,
MousePress = 1 << 3,
MouseRelease = 1 << 4,
MousePos = 1 << 5,
MouseButtonMask= 0b11 << 6,
Scroll = 1 << 8,
Resize = 1 << 9,
MenuSelect = 1 << 10,
};
enum NamedKey : Codepoint
{
// use UTF-16 surrogate pairs range
Backspace = 0xD800,
Delete,
Escape,
Return,
Up,
Down,
Left,
Right,
PageUp,
PageDown,
Home,
End,
Insert,
Tab,
Space,
F1,
F2,
F3,
F4,
F5,
F6,
F7,
F8,
F9,
F10,
F11,
F12,
FocusIn,
FocusOut,
Invalid,
};
Modifiers modifiers = {};
Codepoint key = {};
constexpr Key(Modifiers modifiers, Codepoint key)
: modifiers(modifiers), key(key) {}
constexpr Key(Codepoint key)
: modifiers(Modifiers::None), key(key) {}
constexpr Key() = default;
constexpr uint64_t val() const { return (uint64_t)modifiers << 32 | key; }
constexpr bool operator==(Key other) const { return val() == other.val(); }
constexpr auto operator<=>(Key other) const { return val() <=> other.val(); }
constexpr DisplayCoord coord() const { return {(int)((int32_t) (key & 0xFFFF0000) >> 16), (int)(key & 0x0000FFFF)}; }
constexpr MouseButton mouse_button() { return MouseButton{((int)modifiers & (int)Modifiers::MouseButtonMask) >> 6}; }
constexpr int scroll_amount() { return (int32_t)modifiers >> 16; }
static Modifiers to_modifier(MouseButton button) { return Key::Modifiers{((int)button << 6) & (int)Modifiers::MouseButtonMask}; }
Optional<Codepoint> codepoint() const;
};
constexpr bool with_bit_ops(Meta::Type<Key::Modifiers>) { return true; }
using KeyList = Vector<Key, MemoryDomain::Mapping>;
class String;
class StringView;
KeyList parse_keys(StringView str);
String to_string(Key key);
StringView to_string(Key::MouseButton button);
Key::MouseButton str_to_button(StringView str);
constexpr Key shift(Key key)
{
return { key.modifiers | Key::Modifiers::Shift, key.key };
}
constexpr Key alt(Key key)
{
return { key.modifiers | Key::Modifiers::Alt, key.key };
}
constexpr Key ctrl(Key key)
{
return { key.modifiers | Key::Modifiers::Control, key.key };
}
constexpr Codepoint encode_coord(DisplayCoord coord) { return (Codepoint)(((int)coord.line << 16) | ((int)coord.column & 0x0000FFFF)); }
constexpr Key resize(DisplayCoord dim) { return { Key::Modifiers::Resize, encode_coord(dim) }; }
constexpr size_t hash_value(const Key& key) { return hash_values(key.modifiers, key.key); }
}
#endif // keys_hh_INCLUDED