Add automatic OneShot modifier/layer-shift feature

This adds a new feature to OneShot: it can now (optionally) treat
modifiers and layer-shift keys as automatic OneShot keys, with
functions to enable and disable this feature for modifiers and
layer-shifts independently.

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

@ -45,8 +45,7 @@ EventHandlerResult ActiveModColorEffect::onKeyswitchEvent(
// it will get highlighted. Conditionally (if // it will get highlighted. Conditionally (if
// `highlight_normal_modifiers_` is set), we also highlight // `highlight_normal_modifiers_` is set), we also highlight
// modifier and layer-shift keys. // modifier and layer-shift keys.
if ((key >= Key_LeftControl && key <= Key_RightGui) || if (::OneShot.isModifier() || ::OneShot.isLayerShift()) {
(key.getFlags() == (SYNTHETIC | SWITCH_TO_KEYMAP))) {
mod_key_bits_.set(key_addr); mod_key_bits_.set(key_addr);
} }
} else if (keyToggledOff(key_state)) { } else if (keyToggledOff(key_state)) {

@ -41,6 +41,9 @@ int16_t OneShot::double_tap_time_out = -1;
uint16_t OneShot::stickable_keys_ = -1; uint16_t OneShot::stickable_keys_ = -1;
bool OneShot::auto_modifiers_ = false;
bool OneShot::auto_layers_ = false;
KeyAddrBitfield OneShot::temp_addrs_; KeyAddrBitfield OneShot::temp_addrs_;
KeyAddrBitfield OneShot::glue_addrs_; KeyAddrBitfield OneShot::glue_addrs_;
@ -199,7 +202,9 @@ EventHandlerResult OneShot::onKeyswitchEvent(
if (!temp && !glue) { if (!temp && !glue) {
// This key_addr is not in a OneShot state. // This key_addr is not in a OneShot state.
if (isOneShotKey(key)) { if (isOneShotKey(key) ||
(auto_modifiers_ && isModifier(key)) ||
(auto_layers_ && isLayerShift(key))) {
// Replace the OneShot key with its corresponding normal key. // Replace the OneShot key with its corresponding normal key.
pressKey(key_addr, key); pressKey(key_addr, key);
return EventHandlerResult::ABORT; return EventHandlerResult::ABORT;
@ -382,8 +387,10 @@ Key OneShot::decodeOneShotKey(Key oneshot_key) {
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// Helper functions for sending key events for keys in OneShot states // Helper functions for sending key events for keys in OneShot states
void OneShot::pressKey(KeyAddr key_addr, Key oneshot_key) { void OneShot::pressKey(KeyAddr key_addr, Key key) {
Key key = decodeOneShotKey(oneshot_key); if (isOneShotKey(key)) {
key = decodeOneShotKey(key);
}
prev_key_addr_ = key_addr; prev_key_addr_ = key_addr;
start_time_ = Runtime.millisAtCycleStart(); start_time_ = Runtime.millisAtCycleStart();
temp_addrs_.set(key_addr); temp_addrs_.set(key_addr);
@ -404,8 +411,8 @@ void OneShot::releaseKey(KeyAddr key_addr) {
// Deprecated functions // Deprecated functions
void OneShot::inject(Key key, uint8_t key_state) { void OneShot::inject(Key key, uint8_t key_state) {
if (! isOneShotKey(key)) { if (isOneShotKey(key)) {
return; key = decodeOneShotKey(key);
} }
// Find an idle keyswitch to use for the injected OneShot key and activate // Find an idle keyswitch to use for the injected OneShot key and activate
// it. This is an ugly hack, but it will work. It does mean that whatever key // it. This is an ugly hack, but it will work. It does mean that whatever key
@ -435,11 +442,10 @@ bool OneShot::isModifierActive(Key key) {
return false; return false;
} }
bool OneShot::isActive(Key oneshot_key) { bool OneShot::isActive(Key key) {
if (! isOneShotKey(oneshot_key)) { if (isOneShotKey(key)) {
return false; key = decodeOneShotKey(key);
} }
Key key = decodeOneShotKey(oneshot_key);
for (KeyAddr key_addr : glue_addrs_) { for (KeyAddr key_addr : glue_addrs_) {
if (live_keys[key_addr] == key) { if (live_keys[key_addr] == key) {
return true; return true;
@ -448,11 +454,10 @@ bool OneShot::isActive(Key oneshot_key) {
return false; return false;
} }
bool OneShot::isSticky(Key oneshot_key) { bool OneShot::isSticky(Key key) {
if (! isOneShotKey(oneshot_key)) { if (isOneShotKey(key)) {
return false; key = decodeOneShotKey(key);
} }
Key key = decodeOneShotKey(oneshot_key);
for (KeyAddr key_addr : glue_addrs_) { for (KeyAddr key_addr : glue_addrs_) {
if (live_keys[key_addr] == key && if (live_keys[key_addr] == key &&
!temp_addrs_.read(key_addr)) { !temp_addrs_.read(key_addr)) {

@ -91,6 +91,42 @@ class OneShot : public kaleidoscope::Plugin {
static void disableStickabilityForModifiers(); static void disableStickabilityForModifiers();
static void disableStickabilityForLayers(); static void disableStickabilityForLayers();
static void enableAutoModifiers() {
auto_modifiers_ = true;
}
static void enableAutoLayers() {
auto_layers_ = true;
}
static void enableAutoOneShot() {
enableAutoModifiers();
enableAutoLayers();
}
static void disableAutoModifiers() {
auto_modifiers_ = false;
}
static void disableAutoLayers() {
auto_layers_ = false;
}
static void disableAutoOneShot() {
disableAutoModifiers();
disableAutoLayers();
}
static void toggleAutoModifiers() {
auto_modifiers_ = ! auto_modifiers_;
}
static void toggleAutoLayers() {
auto_layers_ = ! auto_layers_;
}
static void toggleAutoOneShot() {
if (auto_modifiers_ || auto_layers_) {
disableAutoOneShot();
} else {
enableAutoOneShot();
}
}
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
// Global test functions // Global test functions
@ -103,6 +139,9 @@ class OneShot : public kaleidoscope::Plugin {
return (key.getRaw() >= kaleidoscope::ranges::OS_FIRST && return (key.getRaw() >= kaleidoscope::ranges::OS_FIRST &&
key.getRaw() <= kaleidoscope::ranges::OS_LAST); key.getRaw() <= kaleidoscope::ranges::OS_LAST);
} }
static bool isModifier(Key key);
static bool isLayerShift(Key key);
static bool isStickable(Key key); // inline? static bool isStickable(Key key); // inline?
static bool isTemporary(KeyAddr key_addr); // inline? static bool isTemporary(KeyAddr key_addr); // inline?
@ -193,6 +232,8 @@ class OneShot : public kaleidoscope::Plugin {
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
// State variables // State variables
static uint16_t stickable_keys_; static uint16_t stickable_keys_;
static bool auto_modifiers_;
static bool auto_layers_;
static KeyAddrBitfield temp_addrs_; static KeyAddrBitfield temp_addrs_;
static KeyAddrBitfield glue_addrs_; static KeyAddrBitfield glue_addrs_;

Loading…
Cancel
Save