Browse Source

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
xmkb
okke 3 years ago committed by Pete Johanson
parent
commit
cfd0d3d81a
  1. 1
      app/dts/behaviors/sticky_key.dtsi
  2. 2
      app/dts/bindings/behaviors/zmk,behavior-sticky-key.yaml
  3. 9
      app/src/behaviors/behavior_sticky_key.c
  4. 1
      app/tests/sticky-keys/10-callum-mods/events.patterns
  5. 8
      app/tests/sticky-keys/10-callum-mods/keycode_events.snapshot
  6. 43
      app/tests/sticky-keys/10-callum-mods/native_posix.keymap

1
app/dts/behaviors/sticky_key.dtsi

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

2
app/dts/bindings/behaviors/zmk,behavior-sticky-key.yaml

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

9
app/src/behaviors/behavior_sticky_key.c

@ -31,6 +31,7 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
struct behavior_sticky_key_config { struct behavior_sticky_key_config {
uint32_t release_after_ms; uint32_t release_after_ms;
bool quick_release; bool quick_release;
bool ignore_modifiers;
struct zmk_behavior_binding behavior; struct zmk_behavior_binding behavior;
}; };
@ -201,7 +202,7 @@ static int sticky_key_keycode_state_changed_listener(const zmk_event_t *eh) {
continue; 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. // 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) { if (sticky_key->release_at != 0 && ev->timestamp > sticky_key->release_at) {
stop_timer(sticky_key); 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 (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) { if (sticky_key->modified_key_usage_page != 0 || sticky_key->modified_key_keycode != 0) {
// this sticky key is already in use for a keycode // this sticky key is already in use for a keycode
continue; 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 = { \ static struct behavior_sticky_key_config behavior_sticky_key_config_##n = { \
.behavior = ZMK_KEYMAP_EXTRACT_BINDING(0, DT_DRV_INST(n)), \ .behavior = ZMK_KEYMAP_EXTRACT_BINDING(0, DT_DRV_INST(n)), \
.release_after_ms = DT_INST_PROP(n, release_after_ms), \ .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), \ .quick_release = DT_INST_PROP(n, quick_release), \
}; \ }; \
DEVICE_DT_INST_DEFINE(n, behavior_sticky_key_init, device_pm_control_nop, \ DEVICE_DT_INST_DEFINE(n, behavior_sticky_key_init, device_pm_control_nop, \

1
app/tests/sticky-keys/10-callum-mods/events.patterns

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

8
app/tests/sticky-keys/10-callum-mods/keycode_events.snapshot

@ -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

43
app/tests/sticky-keys/10-callum-mods/native_posix.keymap

@ -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)
>;
};
Loading…
Cancel
Save