summaryrefslogtreecommitdiff
path: root/src/keys.hh
blob: 3123f7cc66be5d45c42cddcacdb6d0b7a9e4d4d8 (plain)
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
#ifndef keys_hh_INCLUDED
#define keys_hh_INCLUDED

#include "unicode.hh"
#include "flags.hh"

#include <vector>

namespace Kakoune
{

struct Key
{
    enum class Modifiers
    {
        None    = 0,
        Control = 1 << 0,
        Alt     = 1 << 1,
        ControlAlt = Control | Alt
    };
    enum NamedKey : Codepoint
    {
        // use UTF-16 surrogate pairs range
        Backspace = 0xD800,
        Delete,
        Escape,
        Up,
        Down,
        Left,
        Right,
        PageUp,
        PageDown,
        Home,
        End,
        BackTab,
        F1,
        F2,
        F3,
        F4,
        F5,
        F6,
        F7,
        F8,
        F9,
        F10,
        F11,
        F12,
        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 bool operator==(Key other) const
    { return modifiers == other.modifiers and key == other.key; }

    constexpr bool operator!=(Key other) const
    { return modifiers != other.modifiers or key != other.key; }
};

template<> struct WithBitOps<Key::Modifiers> : std::true_type {};

using KeyList = std::vector<Key>;

class String;
class StringView;

KeyList parse_keys(StringView str);
String  key_to_str(Key key);

constexpr Key alt(Codepoint key) { return { Key::Modifiers::Alt, key }; }
constexpr Key ctrl(Codepoint key) { return { Key::Modifiers::Control, key }; }
constexpr Key ctrlalt(Codepoint key) { return { Key::Modifiers::ControlAlt, key }; }

}

namespace std
{

template<>
struct hash<Kakoune::Key>
{
    size_t operator()(Kakoune::Key key) const
    {
        return static_cast<size_t>(key.modifiers) * 1024 + key.key;
    }
};

}


#endif // keys_hh_INCLUDED