3030
3131namespace kaleidoscope {
3232
33- union Key {
33+ class Key {
3434
35- struct {
36- uint8_t keyCode;
37- uint8_t flags;
38- };
39- uint16_t raw;
35+ public:
36+
37+ typedef uint16_t StorageType;
4038
4139 Key () = default ;
4240
43- constexpr Key (uint8_t __keyCode, uint8_t __flags)
44- : keyCode (__keyCode), flags (__flags) {
41+ constexpr Key (uint16_t raw)
42+ : Key{(uint8_t )(raw & 0x00FF ), (uint8_t )(raw >> 8 )}
43+ {}
44+
45+ constexpr Key (uint8_t key_code, uint8_t flags)
46+ : keyCode{key_code},
47+ flags{flags}
48+ {}
49+
50+ void setFlags (uint8_t new_flags) {
51+ flags.value_ = new_flags;
52+ }
53+ constexpr const uint8_t &getFlags () const {
54+ return flags.value_ ;
55+ }
56+
57+ void setKeyCode (uint8_t key_code) {
58+ keyCode.value_ = key_code;
59+ }
60+ constexpr const uint8_t &getKeyCode () const {
61+ return keyCode.value_ ;
4562 }
4663
47- constexpr Key (uint16_t __raw)
48- : raw (__raw) {
64+ void setRaw (uint16_t raw) {
65+ flags.value_ = (uint8_t )(raw >> 8 );
66+ keyCode.value_ = (uint8_t )(raw & 0x00FF );
67+ }
68+ constexpr uint16_t getRaw () const {
69+ return (uint16_t )(
70+ ((uint16_t )flags.value_ << 8 )
71+ + (uint16_t )keyCode.value_
72+ );
4973 }
5074
51- constexpr bool operator ==(const uint16_t rhs) const {
52- return this ->raw == rhs;
75+ constexpr bool operator ==(const StorageType rhs) const {
76+ return this ->getRaw () == rhs;
5377 }
5478 constexpr bool operator ==(const Key& rhs) const {
55- return this ->raw == rhs.raw ;
79+ return (this ->keyCode .value_ == rhs.keyCode .value_ )
80+ && (this ->flags .value_ == rhs.flags .value_ );
5681 }
57- Key& operator =(const uint16_t raw) {
58- this ->raw = raw ;
82+ Key& operator =(const StorageType raw) {
83+ this ->setRaw ( raw) ;
5984 return *this ;
6085 }
6186 constexpr bool operator !=(const Key& rhs) const {
6287 return !(*this == rhs);
6388 }
64- constexpr bool operator >=(const uint16_t raw) const {
65- return this ->raw >= raw;
89+ constexpr bool operator >=(const StorageType raw) const {
90+ return this ->getRaw () >= raw;
6691 }
67- constexpr bool operator <=(const uint16_t raw) const {
68- return this ->raw <= raw;
92+ constexpr bool operator <=(const StorageType raw) const {
93+ return this ->getRaw () <= raw;
6994 }
70- constexpr bool operator >(const uint16_t raw) const {
71- return this ->raw > raw;
95+ constexpr bool operator >(const StorageType raw) const {
96+ return this ->getRaw () > raw;
7297 }
73- constexpr bool operator <(const uint16_t raw) const {
74- return this ->raw < raw;
98+ constexpr bool operator <(const StorageType raw) const {
99+ return this ->getRaw () < raw;
75100 }
76101 constexpr bool operator >=(const Key& other) const {
77- return this ->raw >= other.raw ;
102+ return this ->getRaw () >= other.getRaw () ;
78103 }
79104 constexpr bool operator <=(const Key& other) const {
80- return this ->raw <= other.raw ;
105+ return this ->getRaw () <= other.getRaw () ;
81106 }
82107 constexpr bool operator >(const Key& other) const {
83- return this ->raw > other.raw ;
108+ return this ->getRaw () > other.getRaw () ;
84109 }
85110 constexpr bool operator <(const Key& other) const {
86- return this ->raw < other.raw ;
111+ return this ->getRaw () < other.getRaw ();
112+ }
113+
114+ Key readFromProgmem () const {
115+ return Key{pgm_read_byte (&(this ->getKeyCode ())),
116+ pgm_read_byte (&(this ->getFlags ()))};
87117 }
118+
119+ // The data proxy objects are required to only emit deprecation
120+ // messages when members 'keyCode' and 'flags' are accessed directly
121+ // but not if accessed by class Key.
122+ //
123+ // Once the deprecation periode elapsed both proxy members 'keyCode'
124+ // and 'flags' of class Key can be converted to private uint8_t members
125+ // of class Key. Class DataProxy can then be safely removed.
126+ //
127+ class DataProxy {
128+
129+ friend class Key ;
130+
131+ public:
132+
133+ DataProxy () = default ;
134+
135+ constexpr DataProxy (uint8_t value) : value_{value} {}
136+
137+ DEPRECATED (DIRECT_KEY_MEMBER_ACCESS)
138+ DataProxy &operator =(uint8_t value) {
139+ value_ = value;
140+ return *this ;
141+ }
142+
143+ DEPRECATED (DIRECT_KEY_MEMBER_ACCESS)
144+ constexpr operator uint8_t () const {
145+ return value_;
146+ }
147+
148+ private:
149+ uint8_t value_;
150+ };
151+
152+ DataProxy keyCode;
153+ DataProxy flags;
88154};
89155
156+ static_assert (sizeof (Key) == 2 , " sizeof(Key) changed" );
157+
158+ // Overload this function to define alternative conversions to type Key.
159+ //
160+ constexpr Key convertToKey (Key k) {
161+ return k;
162+ }
163+
164+ constexpr Key addFlags (Key k, uint8_t add_flags) {
165+ return Key (k.getKeyCode (), k.getFlags () | add_flags);
166+ }
167+
90168} // namespace kaleidoscope
91169
92170// For compatibility reasons make the Key class also available
@@ -105,11 +183,11 @@ typedef kaleidoscope::Key Key_;
105183#define SYNTHETIC B01000000
106184#define RESERVED B10000000
107185
108- #define LCTRL (k ) Key(k.keyCode , k.flags | CTRL_HELD)
109- #define LALT (k ) Key(k.keyCode , k.flags | LALT_HELD)
110- #define RALT (k ) Key(k.keyCode , k.flags | RALT_HELD)
111- #define LSHIFT (k ) Key(k.keyCode , k.flags | SHIFT_HELD)
112- #define LGUI (k ) Key(k.keyCode , k.flags | GUI_HELD)
186+ #define LCTRL (k ) Key(k.getKeyCode() , k.getFlags() | CTRL_HELD)
187+ #define LALT (k ) Key(k.getKeyCode() , k.getFlags() | LALT_HELD)
188+ #define RALT (k ) Key(k.getKeyCode() , k.getFlags() | RALT_HELD)
189+ #define LSHIFT (k ) Key(k.getKeyCode() , k.getFlags() | SHIFT_HELD)
190+ #define LGUI (k ) Key(k.getKeyCode() , k.getFlags() | GUI_HELD)
113191
114192// we assert that synthetic keys can never have keys held, so we reuse the _HELD bits
115193#define IS_SYSCTL B00000001
@@ -153,5 +231,9 @@ typedef kaleidoscope::Key Key_;
153231 use the 10 lsb as the HID Consumer code. If you need to get the keycode of a Consumer key
154232 use the CONSUMER(key) macro this will return the 10bit keycode.
155233*/
156- #define CONSUMER (key ) (key.raw & 0x03FF )
234+ #define CONSUMER (key ) (key.getRaw() & 0x03FF )
157235#define CONSUMER_KEY (code, flags ) Key((code) | ((flags | SYNTHETIC|IS_CONSUMER) << 8 ))
236+
237+ namespace kaleidoscope {
238+ constexpr Key bad_keymap_key{0 , RESERVED};
239+ }
0 commit comments