Adapt TopsyTurvy to keyboard state array

This is an extensive rewrite, but I think it simplifies the logic and makes the
plugin's code easier to follow.

Signed-off-by: Michael Richters <gedankenexperimenter@gmail.com>
pull/1024/head
Michael Richters 4 years ago
parent a956c2d77d
commit 71bf24a503
No known key found for this signature in database
GPG Key ID: 1288FD13E4EEF0C0

@ -21,53 +21,61 @@
namespace kaleidoscope { namespace kaleidoscope {
namespace plugin { namespace plugin {
uint8_t TopsyTurvy::last_pressed_position_; KeyAddr TopsyTurvy::tt_addr_ = KeyAddr::none();
bool TopsyTurvy::is_shifted_; bool TopsyTurvy::shift_detected_ = false;
bool TopsyTurvy::is_active_;
EventHandlerResult TopsyTurvy::onKeyswitchEvent(Key &mapped_key, KeyAddr key_addr, uint8_t key_state) { EventHandlerResult TopsyTurvy::beforeEachCycle() {
// Clear the shift detection state before each scan cycle.
shift_detected_ = false;
return EventHandlerResult::OK;
}
if (mapped_key == Key_LeftShift || EventHandlerResult TopsyTurvy::beforeReportingState() {
mapped_key == Key_RightShift) { // If no TopsyTurvy key is active, there's nothing to do.
is_shifted_ = keyIsPressed(key_state); if (! tt_addr_.isValid())
if (is_active_) return EventHandlerResult::OK;
return EventHandlerResult::EVENT_CONSUMED;
}
if (mapped_key < ranges::TT_FIRST || mapped_key > ranges::TT_LAST) { // A TopsyTurvy key is active. That means we need to reverse the shift state,
if (keyToggledOn(key_state) && (mapped_key < Key_LeftControl || mapped_key > Key_RightGui)) { // whether it was on or off.
last_pressed_position_ = key_addr.toInt(); if (shift_detected_) {
kaleidoscope::Runtime.hid().keyboard().releaseKey(Key_LeftShift);
kaleidoscope::Runtime.hid().keyboard().releaseKey(Key_RightShift);
} else {
kaleidoscope::Runtime.hid().keyboard().pressKey(Key_LeftShift);
} }
return EventHandlerResult::OK;
}
EventHandlerResult TopsyTurvy::onKeyswitchEvent(Key &key,
KeyAddr key_addr,
uint8_t key_state) {
// If a modifer key (including combo modifiers, but not non-modifier keys with
// mod flags) is active, and it includes `shift` (either from its keycode or a
// mod flag), record that we detected an "intentional shift".
if (key.isKeyboardShift() && keyIsPressed(key_state))
shift_detected_ = true;
// If the active TopsyTurvy key toggles off, clear the stored address to
// record that.
if (keyToggledOff(key_state)) {
if (key_addr == tt_addr_) {
tt_addr_.clear();
}
return EventHandlerResult::OK; return EventHandlerResult::OK;
} }
is_active_ = keyIsPressed(key_state);
if (keyToggledOn(key_state)) { if (keyToggledOn(key_state)) {
last_pressed_position_ = key_addr.toInt(); if (isTopsyTurvyKey(key)) {
// If a TopsyTurvy key toggles on, store its address to indicate that it's
// active, and decode its key value to store in the active keys cache.
tt_addr_ = key_addr;
key = Key(key.getRaw() - ranges::TT_FIRST);
} else { } else {
if (last_pressed_position_ != key_addr.toInt()) { // If any other key toggles on, clear the active TopsyTurvy address.
return EventHandlerResult::EVENT_CONSUMED; tt_addr_.clear();
} }
} }
mapped_key.setRaw(mapped_key.getRaw() - ranges::TT_FIRST);
// invert the shift state
if (!is_shifted_) {
mapped_key.setFlags(mapped_key.getFlags() | SHIFT_HELD);
return EventHandlerResult::OK;
}
if (keyIsPressed(key_state)) {
kaleidoscope::Runtime.hid().keyboard().releaseKey(Key_LeftShift);
kaleidoscope::Runtime.hid().keyboard().releaseKey(Key_RightShift);
return EventHandlerResult::OK; return EventHandlerResult::OK;
}
return EventHandlerResult::EVENT_CONSUMED;
} }
} }

@ -29,12 +29,18 @@ class TopsyTurvy: public kaleidoscope::Plugin {
public: public:
TopsyTurvy(void) {} TopsyTurvy(void) {}
EventHandlerResult onKeyswitchEvent(Key &mapped_key, KeyAddr key_addr, uint8_t key_state); EventHandlerResult beforeEachCycle();
EventHandlerResult beforeReportingState();
EventHandlerResult onKeyswitchEvent(Key &key, KeyAddr key_addr, uint8_t key_state);
static bool isTopsyTurvyKey(Key key) {
return (key >= ranges::TT_FIRST &&
key <= ranges::TT_LAST);
}
private: private:
static uint8_t last_pressed_position_; static KeyAddr tt_addr_;
static bool is_shifted_; static bool shift_detected_;
static bool is_active_;
}; };
} }
} }

Loading…
Cancel
Save