From 8471399f1fbbdba20c7ab0221c2e632df370f600 Mon Sep 17 00:00:00 2001 From: Cem Aksoylar Date: Wed, 8 Jan 2025 23:43:43 -0800 Subject: [PATCH] fix(behaviors): Make multiple sticky keys work on same key position --- app/src/behaviors/behavior_sticky_key.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/app/src/behaviors/behavior_sticky_key.c b/app/src/behaviors/behavior_sticky_key.c index 3faeec53a39..8e495dee039 100644 --- a/app/src/behaviors/behavior_sticky_key.c +++ b/app/src/behaviors/behavior_sticky_key.c @@ -85,12 +85,18 @@ static struct active_sticky_key *store_sticky_key(struct zmk_behavior_binding_ev } static void clear_sticky_key(struct active_sticky_key *sticky_key) { + LOG_DBG("clearing sticky key at position %d, param %d", sticky_key->position, + sticky_key->param1); sticky_key->position = ZMK_BHV_STICKY_KEY_POSITION_FREE; } -static struct active_sticky_key *find_sticky_key(uint32_t position) { +static struct active_sticky_key * +find_sticky_key(uint32_t position, struct zmk_behavior_binding behavior, uint32_t binding_param) { for (int i = 0; i < ZMK_BHV_STICKY_KEY_MAX_HELD; i++) { - if (active_sticky_keys[i].position == position && !active_sticky_keys[i].timer_cancelled) { + if (active_sticky_keys[i].position == position && + active_sticky_keys[i].config->behavior.behavior_dev == behavior.behavior_dev && + active_sticky_keys[i].param1 == binding_param && + !active_sticky_keys[i].timer_cancelled) { return &active_sticky_keys[i]; } } @@ -156,8 +162,9 @@ static int on_sticky_key_binding_pressed(struct zmk_behavior_binding *binding, const struct device *dev = zmk_behavior_get_binding(binding->behavior_dev); const struct behavior_sticky_key_config *cfg = dev->config; struct active_sticky_key *sticky_key; - sticky_key = find_sticky_key(event.position); + sticky_key = find_sticky_key(event.position, cfg->behavior, binding->param1); if (sticky_key != NULL) { + LOG_DBG("found same sticky key pressed at position %d, release it first", event.position); stop_timer(sticky_key); release_sticky_key_behavior(sticky_key, event.timestamp); } @@ -178,7 +185,10 @@ static int on_sticky_key_binding_pressed(struct zmk_behavior_binding *binding, static int on_sticky_key_binding_released(struct zmk_behavior_binding *binding, struct zmk_behavior_binding_event event) { - struct active_sticky_key *sticky_key = find_sticky_key(event.position); + const struct device *dev = zmk_behavior_get_binding(binding->behavior_dev); + const struct behavior_sticky_key_config *cfg = dev->config; + struct active_sticky_key *sticky_key = + find_sticky_key(event.position, cfg->behavior, binding->param1); if (sticky_key == NULL) { LOG_ERR("ACTIVE STICKY KEY CLEARED TOO EARLY"); return ZMK_BEHAVIOR_OPAQUE;