Behaviors: Add 'ignore-modifiers' option to sticky keys

To combine multiple sticky modifiers, the sticky keys must ignore
other (sticky) modifier keypresses.

This behavior is important for "callum-style mods", where all modifiers
are sticky mods.

Fixes #829
This commit is contained in:
okke 2022-01-26 20:42:14 +01:00 committed by Pete Johanson
parent c18c3d9106
commit cfd0d3d81a
6 changed files with 63 additions and 1 deletions

View file

@ -12,6 +12,7 @@
#binding-cells = <1>;
release-after-ms = <1000>;
bindings = <&kp>;
ignore-modifiers;
};
/omit-if-no-ref/ sl: behavior_sticky_layer {
compatible = "zmk,behavior-sticky-key";

View file

@ -15,3 +15,5 @@ properties:
type: int
quick-release:
type: boolean
ignore-modifiers:
type: boolean

View file

@ -31,6 +31,7 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
struct behavior_sticky_key_config {
uint32_t release_after_ms;
bool quick_release;
bool ignore_modifiers;
struct zmk_behavior_binding behavior;
};
@ -201,7 +202,7 @@ static int sticky_key_keycode_state_changed_listener(const zmk_event_t *eh) {
continue;
}
// If events were queued, the timer event may be queued late or not at all.
// If this event was queued, the timer may be triggered late or not at all.
// Release the sticky key if the timer should've run out in the meantime.
if (sticky_key->release_at != 0 && ev->timestamp > sticky_key->release_at) {
stop_timer(sticky_key);
@ -210,6 +211,11 @@ static int sticky_key_keycode_state_changed_listener(const zmk_event_t *eh) {
}
if (ev->state) { // key down
if (sticky_key->config->ignore_modifiers && is_mod(ev->usage_page, ev->keycode)) {
// ignore modifier key press so we can stack sticky keys and combine with other
// modifiers
continue;
}
if (sticky_key->modified_key_usage_page != 0 || sticky_key->modified_key_keycode != 0) {
// this sticky key is already in use for a keycode
continue;
@ -270,6 +276,7 @@ static struct behavior_sticky_key_data behavior_sticky_key_data;
static struct behavior_sticky_key_config behavior_sticky_key_config_##n = { \
.behavior = ZMK_KEYMAP_EXTRACT_BINDING(0, DT_DRV_INST(n)), \
.release_after_ms = DT_INST_PROP(n, release_after_ms), \
.ignore_modifiers = DT_INST_PROP(n, ignore_modifiers), \
.quick_release = DT_INST_PROP(n, quick_release), \
}; \
DEVICE_DT_INST_DEFINE(n, behavior_sticky_key_init, device_pm_control_nop, \

View file

@ -0,0 +1 @@
s/.*hid_listener_keycode_//p

View file

@ -0,0 +1,8 @@
pressed: usage_page 0x07 keycode 0xe0 implicit_mods 0x00 explicit_mods 0x00
pressed: usage_page 0x07 keycode 0xe1 implicit_mods 0x00 explicit_mods 0x00
pressed: usage_page 0x07 keycode 0x04 implicit_mods 0x00 explicit_mods 0x00
released: usage_page 0x07 keycode 0xe0 implicit_mods 0x00 explicit_mods 0x00
released: usage_page 0x07 keycode 0xe1 implicit_mods 0x00 explicit_mods 0x00
released: usage_page 0x07 keycode 0x04 implicit_mods 0x00 explicit_mods 0x00
pressed: usage_page 0x07 keycode 0x04 implicit_mods 0x00 explicit_mods 0x00
released: usage_page 0x07 keycode 0x04 implicit_mods 0x00 explicit_mods 0x00

View file

@ -0,0 +1,43 @@
#include <dt-bindings/zmk/keys.h>
#include <behaviors.dtsi>
#include <dt-bindings/zmk/kscan_mock.h>
/ {
keymap {
compatible = "zmk,keymap";
label ="Default keymap";
default_layer {
bindings = <
&sk E &sl 1
&kp A &kp B>;
};
lower_layer {
bindings = <
&sk LEFT_CONTROL &kp X
&sk LEFT_SHIFT &kp Z>;
};
};
};
&kscan {
events = <
/* press sl lower_layer */
ZMK_MOCK_PRESS(0,1,10)
/* tap sk LEFT_CONTROL */
ZMK_MOCK_PRESS(0,0,10)
ZMK_MOCK_RELEASE(0,0,10)
/* tap sk LEFT_SHIFT */
ZMK_MOCK_PRESS(1,0,10)
ZMK_MOCK_RELEASE(1,0,10)
/* release sl lower_layer */
ZMK_MOCK_RELEASE(0,1,10)
/* tap A (with left control and left shift enabled) */
ZMK_MOCK_PRESS(1,0,10)
ZMK_MOCK_RELEASE(1,0,10)
/* tap A (no sticky keys anymore) */
ZMK_MOCK_PRESS(1,0,10)
ZMK_MOCK_RELEASE(1,0,10)
>;
};