From 2faa7c00a956cf2de370544a1557d129ba91140d Mon Sep 17 00:00:00 2001 From: Michael Richters Date: Sat, 29 May 2021 10:31:10 -0500 Subject: [PATCH] Add a "no-delay" mode to SpaceCadet SpaceCadet now has three "modes": on, off, and on with no delay. In "no-delay" mode, when a SpaceCadet key is pressed, its primary (modifier) key value is sent immediately to the host, and if it is released before timing out, that value is then replaced by the configured "tap" value of the key. Signed-off-by: Michael Richters --- .../src/kaleidoscope/plugin/SpaceCadet.cpp | 15 +++++++++++++-- .../src/kaleidoscope/plugin/SpaceCadet.h | 5 ++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/plugins/Kaleidoscope-SpaceCadet/src/kaleidoscope/plugin/SpaceCadet.cpp b/plugins/Kaleidoscope-SpaceCadet/src/kaleidoscope/plugin/SpaceCadet.cpp index 4e091993..b3fd2edb 100644 --- a/plugins/Kaleidoscope-SpaceCadet/src/kaleidoscope/plugin/SpaceCadet.cpp +++ b/plugins/Kaleidoscope-SpaceCadet/src/kaleidoscope/plugin/SpaceCadet.cpp @@ -117,7 +117,7 @@ EventHandlerResult SpaceCadet::onKeyswitchEvent(KeyEvent &event) { } // Do nothing if disabled, but keep the event tracker current. - if (mode_ != Mode::ON) + if (mode_ == Mode::OFF) return EventHandlerResult::OK; if (!event_queue_.isEmpty()) { @@ -145,7 +145,13 @@ EventHandlerResult SpaceCadet::onKeyswitchEvent(KeyEvent &event) { // Check for a SpaceCadet key pending_map_index_ = getSpaceCadetKeyIndex(event.key); if (pending_map_index_ >= 0) { - // A SpaceCadet key was pressed + // A SpaceCadet key has just toggled on. First, if we're in no-delay mode, + // we need to send the event unchanged (with the primary `Key` value), + // bypassing other `onKeyswitchEvent()` handlers. + if (mode_ == Mode::NO_DELAY) + Runtime.handleKeyEvent(event); + // Queue the press event and abort; this press event will be resolved + // later. event_queue_.append(event); return EventHandlerResult::ABORT; } @@ -194,6 +200,11 @@ void SpaceCadet::flushQueue() { void SpaceCadet::flushEvent(bool is_tap) { KeyEvent event = event_queue_.event(0); if (is_tap && pending_map_index_ >= 0) { + // If we're in no-delay mode, we should first send the release of the + // modifier key as a courtesy before sending the tap event. + if (mode_ == Mode::NO_DELAY) { + Runtime.handleKeyEvent(KeyEvent(event.addr, WAS_PRESSED)); + } event.key = map[pending_map_index_].output; } event_queue_.shift(); diff --git a/plugins/Kaleidoscope-SpaceCadet/src/kaleidoscope/plugin/SpaceCadet.h b/plugins/Kaleidoscope-SpaceCadet/src/kaleidoscope/plugin/SpaceCadet.h index 23453120..3365d755 100644 --- a/plugins/Kaleidoscope-SpaceCadet/src/kaleidoscope/plugin/SpaceCadet.h +++ b/plugins/Kaleidoscope-SpaceCadet/src/kaleidoscope/plugin/SpaceCadet.h @@ -66,8 +66,11 @@ class SpaceCadet : public kaleidoscope::Plugin { static void disable() { mode_ = Mode::OFF; } + static void enableWithoutDelay() { + mode_ = Mode::NO_DELAY; + } static bool active() { - return (mode_ == Mode::ON); + return (mode_ == Mode::ON || mode_ == Mode::NO_DELAY); } // Publically accessible variables