Skip to content

Commit 552770f

Browse files
author
Florian Fleissner
committed
Added top-Key conversion functions
Two new functions have been introduced in namespace kaleidoscope. One to conveniently add keyflags to an existing Key variable and another one that can be overloaded to convert other types to type Key. The keymap definition macros and the modifier function macro (LCTRL, LALT, ...) are now using the to-Key conversion functions. This allows users to use alternative ways to define keymaps by defining types of their own that automatically convert to type Key. Signed-off-by: Florian Fleissner <[email protected]>
1 parent 0a18051 commit 552770f

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

src/kaleidoscope/Kaleidoscope.h

+30-2
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,40 @@ void setup();
4343

4444
static constexpr DEPRECATED(KEYBOARDHARDWARE) kaleidoscope::Device &KeyboardHardware = kaleidoscope_internal::device;
4545

46+
// Note: The CONVERT_TO_KEY macro can be redefined to use different
47+
// host_keymap-keymaps on different layers (see key_defs.h for its
48+
// default definition.
49+
50+
#define CONVERT_AND_CHECK_KEY(KEY) \
51+
( \
52+
( \
53+
struct { \
54+
static_assert(CONVERT_TO_KEY(KEY) != kaleidoscope::bad_keymap_key, \
55+
"Bad key definition: \'" #KEY "\'"); \
56+
} \
57+
){}, \
58+
CONVERT_TO_KEY(KEY) \
59+
)
60+
4661
#ifdef PER_KEY_DATA_STACKED
47-
#define KEYMAP_STACKED(...) { PER_KEY_DATA_STACKED(XXX, __VA_ARGS__) }
62+
#define KEYMAP_STACKED(...) \
63+
{ \
64+
MAP_LIST( \
65+
CONVERT_AND_CHECK_KEY, \
66+
PER_KEY_DATA_STACKED(XXX, __VA_ARGS__) \
67+
) \
68+
}
69+
4870
#endif
4971

5072
#ifdef PER_KEY_DATA
51-
#define KEYMAP(...) { PER_KEY_DATA(XXX, __VA_ARGS__) }
73+
#define KEYMAP(...) \
74+
{ \
75+
MAP_LIST( \
76+
CONVERT_AND_CHECK_KEY, \
77+
PER_KEY_DATA(XXX, __VA_ARGS__) \
78+
) \
79+
}
5280
#endif
5381

5482
static constexpr DEPRECATED(ROWS) uint8_t ROWS = kaleidoscope_internal::device.matrix_rows;

src/kaleidoscope/key_defs.h

+12-5
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,13 @@ constexpr Key addFlags(Key k, uint8_t add_flags) {
167167

168168
} // namespace kaleidoscope
169169

170+
// Redefine this macro to enable using alternative char-string to Key
171+
// conversions per layer. This is, howerver, only necessary in rare cases
172+
// (e.g. to use individual host_keymap language maps on
173+
// different layers, see kaleidoscope/host_keymap/host_keymap.h for an example).
174+
//
175+
#define CONVERT_TO_KEY(INPUT) kaleidoscope::convertToKey(INPUT)
176+
170177
// For compatibility reasons make the Key class also available
171178
// in global namespace.
172179
//
@@ -183,11 +190,11 @@ typedef kaleidoscope::Key Key_;
183190
#define SYNTHETIC B01000000
184191
#define RESERVED B10000000
185192

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)
193+
#define LCTRL(k) kaleidoscope::addFlags(CONVERT_TO_KEY(k), CTRL_HELD)
194+
#define LALT(k) kaleidoscope::addFlags(CONVERT_TO_KEY(k), LALT_HELD)
195+
#define RALT(k) kaleidoscope::addFlags(CONVERT_TO_KEY(k), RALT_HELD)
196+
#define LSHIFT(k) kaleidoscope::addFlags(CONVERT_TO_KEY(k), SHIFT_HELD)
197+
#define LGUI(k) kaleidoscope::addFlags(CONVERT_TO_KEY(k), GUI_HELD)
191198

192199
// we assert that synthetic keys can never have keys held, so we reuse the _HELD bits
193200
#define IS_SYSCTL B00000001

0 commit comments

Comments
 (0)