Hi,
TL;DR;
I’m seeking feedback on my first plugin code before making it better for sharing, see code sample below
Long Story
I own a model 100 since a few months, and I discover the world of split keyboards and layer.
Since I type both in english and french (and c++ and other programming languages), I need to have efficient way to type accented characters.
Firstly I setted up macro for compose key combination, within Chrysalis, but while the key to macro id mapping is exported, the macro content is not (I have one kbd at home, another at work and I want to share the configuration).
So I think the solution is to setup my “accent layer” on the firmware side, which I’m currently doing.
To this end I have done a few tests, and I’m writing here to have feedback.
I have developped a plugin that leverage Unicode plugin to type accent char (which seems to be portable accros OSes, not tested yet).
Sample code
First I define key codes
constexpr Key Key_A_CIRC{kaleidoscope::ranges::SAFE_START};
constexpr Key Key_A_ACUTE{Key_A_CIRC.getRaw() + 1};
// ...
Then I created a table key code to unicode char
struct KeyToUnicode {
Key key;
uint32_t unicode;
};
static const KeyToUnicode keyToUnicodeTable[] = {
{Key_A_CIRC, U'â'},
{Key_A_ACUTE, U'á'},
// ...
Finally I setted up the plugin as (temporary name, “Weur” inspired by https://altgr-weur.eu/altgr-intl.html)
class WeurPlugin : public Plugin {
public:
static constexpr uint32_t upperOffset = 0x00E0 - 0x00C0;
bool isShiftKeyHeld(Key &which) {
for (Key key : kaleidoscope::live_keys.all()) {
if (key.isKeyboardShift()) {
which = key;
return true;
}
}
return false;
}
EventHandlerResult onKeyEvent(KeyEvent &event) {
uint32_t converted = keyToUnicode(event.key);
if (converted == 0) return EventHandlerResult::OK;
if (keyToggledOn(event.state)) {
Key shiftKey;
if (isShiftKeyHeld(shiftKey)) {
// I'm not sure if its the right way to do that,
// having shift pressed while using Unicode.type produces strange results
kaleidoscope::Runtime.hid().keyboard().releaseRawKey(shiftKey);
kaleidoscope::Runtime.hid().keyboard().sendReport();
::Unicode.type(converted - upperOffset);
// upperOffset works most of the time, need better managment
kaleidoscope::Runtime.hid().keyboard().pressRawKey(shiftKey);
kaleidoscope::Runtime.hid().keyboard().sendReport();
} else {
::Unicode.type(converted);
}
}
return EventHandlerResult::ABORT;
}
};
In the layer definition, I can use Key_A_CIRC where ever I want e.g. as Key_A
It supports helded shift to produce uppercase accented character.
The main drawback is a visual feedback on screen, I can quickly see the typed sequence for unicode char (that was also the case with macro and compose key in chrysalis).
Any comment welcome to improve the approach.
++dlyr