Skip to content

Commit

Permalink
use no-sticky-after-hold-ms
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyendown committed Jun 30, 2023
1 parent bea64a9 commit b8f52da
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 14 deletions.
2 changes: 1 addition & 1 deletion app/dts/bindings/behaviors/zmk,behavior-sticky-key.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ properties:
release-after-ms:
type: int
required: true
deactivate-from-hold-ms:
no-sticky-after-hold-ms:
type: int
quick-release:
type: boolean
Expand Down
16 changes: 8 additions & 8 deletions app/src/behaviors/behavior_sticky_key.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);

struct behavior_sticky_key_config {
uint32_t release_after_ms;
uint32_t deactivate_from_hold_ms;
uint32_t no_sticky_after_hold_ms;
bool quick_release;
bool ignore_modifiers;
struct zmk_behavior_binding behavior;
Expand All @@ -42,7 +42,7 @@ struct active_sticky_key {
uint32_t param2;
const struct behavior_sticky_key_config *config;
// timer data.
int64_t deactivate_from_hold_at;
int64_t no_sticky_after_hold_at;
bool timer_started;
bool timer_cancelled;
int64_t release_at;
Expand All @@ -68,7 +68,7 @@ static struct active_sticky_key *store_sticky_key(uint32_t position, uint32_t pa
sticky_key->param2 = param2;
sticky_key->config = config;
sticky_key->release_at = 0;
sticky_key->deactivate_from_hold_at = 0;
sticky_key->no_sticky_after_hold_at = 0;
sticky_key->timer_cancelled = false;
sticky_key->timer_started = false;
sticky_key->modified_key_usage_page = 0;
Expand Down Expand Up @@ -147,7 +147,7 @@ static int on_sticky_key_binding_pressed(struct zmk_behavior_binding *binding,
return ZMK_BEHAVIOR_OPAQUE;
}

sticky_key->deactivate_from_hold_at = event.timestamp + cfg->deactivate_from_hold_ms;
sticky_key->no_sticky_after_hold_at = event.timestamp + cfg->no_sticky_after_hold_ms;

press_sticky_key_behavior(sticky_key, event.timestamp);
LOG_DBG("%d new sticky_key", event.position);
Expand All @@ -167,8 +167,8 @@ static int on_sticky_key_binding_released(struct zmk_behavior_binding *binding,
return release_sticky_key_behavior(sticky_key, event.timestamp);
}

if (sticky_key->deactivate_from_hold_at < event.timestamp) {
LOG_DBG("Sticky key %d was held for longer than deactivate-from-hold-ms.", event.position);
if (sticky_key->no_sticky_after_hold_at < event.timestamp) {
LOG_DBG("Sticky key %d was held for longer than no-sticky-after-hold-ms.", event.position);
return release_sticky_key_behavior(sticky_key, event.timestamp);
}

Expand Down Expand Up @@ -300,8 +300,8 @@ 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), \
.deactivate_from_hold_ms = \
DT_INST_PROP_OR(n, deactivate_from_hold_ms, DT_INST_PROP(n, release_after_ms)), \
.no_sticky_after_hold_ms = \
DT_INST_PROP_OR(n, no_sticky_after_hold_ms, DT_INST_PROP(n, release_after_ms)), \
.ignore_modifiers = DT_INST_PROP(n, ignore_modifiers), \
.quick_release = DT_INST_PROP(n, quick_release), \
}; \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@
#include "../behavior_keymap.dtsi"

/* This test ensures that sticky keys are deactivated immediately upon
* release after being held for longer than release-after-ms.
* release after being held for longer than `no-sticky-after-hold-ms`.
*/

&sk {
no-sticky-after-hold-ms = <200>;
};

&sl {
no-sticky-after-hold-ms = <200>;
};

/ {
keymap {
compatible = "zmk,keymap";
Expand All @@ -28,17 +36,17 @@

&kscan {
events = <
// Hold &sk E past release-after-ms
ZMK_MOCK_PRESS(0,0,1200)
// Hold &sk E past no-sticky-after-hold-ms
ZMK_MOCK_PRESS(0,0,400)
// Release &sk E
ZMK_MOCK_RELEASE(0,0,10)
// &sk E should now be deactivated
// Tap &kp A right after
ZMK_MOCK_PRESS(1,0,10)
ZMK_MOCK_RELEASE(1,0,10)

// Hold &sl 1 past release-after-ms
ZMK_MOCK_PRESS(0,1,1200)
// Hold &sl 1 past no-sticky-after-hold-ms
ZMK_MOCK_PRESS(0,1,400)
// Release &sl 1
ZMK_MOCK_RELEASE(0,1,10)
// &sl 1 should now be deactivated
Expand Down

0 comments on commit b8f52da

Please sign in to comment.