Skip to content

Commit 631f4d4

Browse files
committed
feat(behaviors): hold while undecided
1 parent 864394b commit 631f4d4

16 files changed

+304
-32
lines changed

app/dts/bindings/behaviors/zmk,behavior-hold-tap.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ properties:
3131
- "balanced"
3232
- "tap-preferred"
3333
- "tap-unless-interrupted"
34+
hold-while-undecided:
35+
type: boolean
36+
hold-while-undecided-linger:
37+
type: boolean
3438
retro-tap:
3539
type: boolean
3640
hold-trigger-key-positions:

app/src/behaviors/behavior_hold_tap.c

+84-21
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ enum status {
4545
};
4646

4747
enum decision_moment {
48+
HT_KEY_DOWN,
4849
HT_KEY_UP,
4950
HT_OTHER_KEY_DOWN,
5051
HT_OTHER_KEY_UP,
@@ -59,6 +60,8 @@ struct behavior_hold_tap_config {
5960
int quick_tap_ms;
6061
bool global_quick_tap;
6162
enum flavor flavor;
63+
bool hold_while_undecided;
64+
bool hold_while_undecided_deferred;
6265
bool retro_tap;
6366
bool hold_trigger_on_release;
6467
int32_t hold_trigger_key_positions_len;
@@ -358,47 +361,87 @@ static inline const char *decision_moment_str(enum decision_moment decision_mome
358361
}
359362
}
360363

361-
static int press_binding(struct active_hold_tap *hold_tap) {
362-
if (hold_tap->config->retro_tap && hold_tap->status == STATUS_HOLD_TIMER) {
363-
return 0;
364-
}
364+
static int press_hold_binding(struct active_hold_tap *hold_tap) {
365+
struct zmk_behavior_binding_event event = {
366+
.position = hold_tap->position,
367+
.timestamp = hold_tap->timestamp,
368+
};
365369

370+
struct zmk_behavior_binding binding = {0};
371+
binding.behavior_dev = hold_tap->config->hold_behavior_dev;
372+
binding.param1 = hold_tap->param_hold;
373+
return behavior_keymap_binding_pressed(&binding, event);
374+
}
375+
376+
static int press_tap_binding(struct active_hold_tap *hold_tap) {
366377
struct zmk_behavior_binding_event event = {
367378
.position = hold_tap->position,
368379
.timestamp = hold_tap->timestamp,
369380
};
370381

371382
struct zmk_behavior_binding binding = {0};
372-
if (hold_tap->status == STATUS_HOLD_TIMER || hold_tap->status == STATUS_HOLD_INTERRUPT) {
373-
binding.behavior_dev = hold_tap->config->hold_behavior_dev;
374-
binding.param1 = hold_tap->param_hold;
375-
} else {
376-
binding.behavior_dev = hold_tap->config->tap_behavior_dev;
377-
binding.param1 = hold_tap->param_tap;
378-
store_last_hold_tapped(hold_tap);
379-
}
383+
binding.behavior_dev = hold_tap->config->tap_behavior_dev;
384+
binding.param1 = hold_tap->param_tap;
385+
store_last_hold_tapped(hold_tap);
380386
return behavior_keymap_binding_pressed(&binding, event);
381387
}
382388

383-
static int release_binding(struct active_hold_tap *hold_tap) {
384-
if (hold_tap->config->retro_tap && hold_tap->status == STATUS_HOLD_TIMER) {
385-
return 0;
386-
}
389+
static int release_hold_binding(struct active_hold_tap *hold_tap) {
390+
struct zmk_behavior_binding_event event = {
391+
.position = hold_tap->position,
392+
.timestamp = hold_tap->timestamp,
393+
};
387394

395+
struct zmk_behavior_binding binding = {0};
396+
binding.behavior_dev = hold_tap->config->hold_behavior_dev;
397+
binding.param1 = hold_tap->param_hold;
398+
return behavior_keymap_binding_released(&binding, event);
399+
}
400+
401+
static int release_tap_binding(struct active_hold_tap *hold_tap) {
388402
struct zmk_behavior_binding_event event = {
389403
.position = hold_tap->position,
390404
.timestamp = hold_tap->timestamp,
391405
};
392406

393407
struct zmk_behavior_binding binding = {0};
408+
binding.behavior_dev = hold_tap->config->tap_behavior_dev;
409+
binding.param1 = hold_tap->param_tap;
410+
return behavior_keymap_binding_released(&binding, event);
411+
}
412+
413+
static int press_binding(struct active_hold_tap *hold_tap) {
414+
if (hold_tap->config->retro_tap && hold_tap->status == STATUS_HOLD_TIMER) {
415+
return 0;
416+
}
417+
394418
if (hold_tap->status == STATUS_HOLD_TIMER || hold_tap->status == STATUS_HOLD_INTERRUPT) {
395-
binding.behavior_dev = hold_tap->config->hold_behavior_dev;
396-
binding.param1 = hold_tap->param_hold;
419+
if (hold_tap->config->hold_while_undecided) {
420+
// the hold is already active, so we don't need to press it again
421+
return 0;
422+
} else {
423+
return press_hold_binding(hold_tap);
424+
}
397425
} else {
398-
binding.behavior_dev = hold_tap->config->tap_behavior_dev;
399-
binding.param1 = hold_tap->param_tap;
426+
if (hold_tap->config->hold_while_undecided &&
427+
!hold_tap->config->hold_while_undecided_deferred) {
428+
// time to release the hold before pressing the tap
429+
release_hold_binding(hold_tap);
430+
}
431+
return press_tap_binding(hold_tap);
432+
}
433+
}
434+
435+
static int release_binding(struct active_hold_tap *hold_tap) {
436+
if (hold_tap->config->retro_tap && hold_tap->status == STATUS_HOLD_TIMER) {
437+
return 0;
438+
}
439+
440+
if (hold_tap->status == STATUS_HOLD_TIMER || hold_tap->status == STATUS_HOLD_INTERRUPT) {
441+
return release_hold_binding(hold_tap);
442+
} else {
443+
return release_tap_binding(hold_tap);
400444
}
401-
return behavior_keymap_binding_released(&binding, event);
402445
}
403446

404447
static bool is_first_other_key_pressed_trigger_key(struct active_hold_tap *hold_tap) {
@@ -445,6 +488,12 @@ static void decide_hold_tap(struct active_hold_tap *hold_tap,
445488
return;
446489
}
447490

491+
if (hold_tap->config->hold_while_undecided && decision_moment == HT_KEY_DOWN) {
492+
LOG_DBG("%d hold behavior pressed while undecided", hold_tap->position);
493+
press_hold_binding(hold_tap);
494+
return;
495+
}
496+
448497
// If the hold-tap behavior is still undecided, attempt to decide it.
449498
switch (hold_tap->config->flavor) {
450499
case FLAVOR_HOLD_PREFERRED:
@@ -532,6 +581,8 @@ static int on_hold_tap_binding_pressed(struct zmk_behavior_binding *binding,
532581
decide_hold_tap(hold_tap, HT_QUICK_TAP);
533582
}
534583

584+
decide_hold_tap(hold_tap, HT_KEY_DOWN);
585+
535586
// if this behavior was queued we have to adjust the timer to only
536587
// wait for the remaining time.
537588
int32_t tapping_term_ms_left = (hold_tap->timestamp + cfg->tapping_term_ms) - k_uptime_get();
@@ -559,6 +610,10 @@ static int on_hold_tap_binding_released(struct zmk_behavior_binding *binding,
559610
decide_retro_tap(hold_tap);
560611
release_binding(hold_tap);
561612

613+
if (hold_tap->config->hold_while_undecided && hold_tap->config->hold_while_undecided_deferred) {
614+
release_hold_binding(hold_tap);
615+
}
616+
562617
if (work_cancel_result == -EINPROGRESS) {
563618
// let the timer handler clean up
564619
// if we'd clear now, the timer may call back for an uninitialized active_hold_tap.
@@ -652,6 +707,12 @@ static int keycode_state_changed_listener(const zmk_event_t *eh) {
652707
return ZMK_EV_EVENT_BUBBLE;
653708
}
654709

710+
// hold-while-undecided can produce a mod, but we don't want to capture it.
711+
if (undecided_hold_tap->config->hold_while_undecided &&
712+
undecided_hold_tap->status == STATUS_UNDECIDED) {
713+
return ZMK_EV_EVENT_BUBBLE;
714+
}
715+
655716
// only key-up events will bubble through position_state_changed_listener
656717
// if a undecided_hold_tap is active.
657718
LOG_DBG("%d capturing 0x%02X %s event", undecided_hold_tap->position, ev->keycode,
@@ -705,6 +766,8 @@ static int behavior_hold_tap_init(const struct device *dev) {
705766
.quick_tap_ms = DT_INST_PROP(n, quick_tap_ms), \
706767
.global_quick_tap = DT_INST_PROP(n, global_quick_tap), \
707768
.flavor = DT_ENUM_IDX(DT_DRV_INST(n), flavor), \
769+
.hold_while_undecided = DT_INST_PROP(n, hold_while_undecided), \
770+
.hold_while_undecided_deferred = DT_INST_PROP(n, hold_while_undecided_deferred), \
708771
.retro_tap = DT_INST_PROP(n, retro_tap), \
709772
.hold_trigger_on_release = DT_INST_PROP(n, hold_trigger_on_release), \
710773
.hold_trigger_key_positions = DT_INST_PROP(n, hold_trigger_key_positions), \
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
s/.*hid_listener_keycode/kp/p
2+
s/.*mo_keymap_binding/mo/p
3+
s/.*on_hold_tap_binding/ht_binding/p
4+
s/.*decide_hold_tap/ht_decide/p
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
ht_binding_pressed: 0 new undecided hold_tap
2+
ht_decide: 0 hold behavior pressed while undecided
3+
kp_pressed: usage_page 0x07 keycode 0xE1 implicit_mods 0x00 explicit_mods 0x00
4+
ht_decide: 0 decided tap (balanced decision moment key-up)
5+
kp_released: usage_page 0x07 keycode 0xE1 implicit_mods 0x00 explicit_mods 0x00
6+
kp_pressed: usage_page 0x07 keycode 0x04 implicit_mods 0x00 explicit_mods 0x00
7+
kp_released: usage_page 0x07 keycode 0x04 implicit_mods 0x00 explicit_mods 0x00
8+
ht_binding_released: 0 cleaning up hold-tap
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <dt-bindings/zmk/keys.h>
2+
#include <behaviors.dtsi>
3+
#include <dt-bindings/zmk/kscan_mock.h>
4+
5+
/ {
6+
behaviors {
7+
ht_bal: behavior_hold_tap_balanced {
8+
compatible = "zmk,behavior-hold-tap";
9+
label = "HOLD_TAP_BALANCED";
10+
#binding-cells = <2>;
11+
flavor = "balanced";
12+
tapping-term-ms = <300>;
13+
quick-tap-ms = <200>;
14+
bindings = <&kp>, <&kp>;
15+
hold-while-undecided;
16+
};
17+
};
18+
19+
keymap {
20+
compatible = "zmk,keymap";
21+
label ="Default keymap";
22+
23+
default_layer {
24+
bindings = <
25+
&ht_bal LEFT_SHIFT A &ht_bal LEFT_CONTROL B
26+
&kp D &kp RIGHT_CONTROL>;
27+
};
28+
};
29+
};
30+
31+
&kscan {
32+
events = <
33+
ZMK_MOCK_PRESS(0,0,10)
34+
ZMK_MOCK_RELEASE(0,0,10)
35+
>;
36+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
s/.*hid_listener_keycode/kp/p
2+
s/.*mo_keymap_binding/mo/p
3+
s/.*on_hold_tap_binding/ht_binding/p
4+
s/.*decide_hold_tap/ht_decide/p
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
ht_binding_pressed: 0 new undecided hold_tap
2+
ht_decide: 0 hold behavior pressed while undecided
3+
kp_pressed: usage_page 0x07 keycode 0xE1 implicit_mods 0x00 explicit_mods 0x00
4+
ht_decide: 0 decided hold-timer (balanced decision moment timer)
5+
kp_released: usage_page 0x07 keycode 0xE1 implicit_mods 0x00 explicit_mods 0x00
6+
ht_binding_released: 0 cleaning up hold-tap
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <dt-bindings/zmk/keys.h>
2+
#include <behaviors.dtsi>
3+
#include <dt-bindings/zmk/kscan_mock.h>
4+
5+
/ {
6+
behaviors {
7+
ht_bal: behavior_hold_tap_balanced {
8+
compatible = "zmk,behavior-hold-tap";
9+
label = "HOLD_TAP_BALANCED";
10+
#binding-cells = <2>;
11+
flavor = "balanced";
12+
tapping-term-ms = <100>;
13+
quick-tap-ms = <200>;
14+
bindings = <&kp>, <&kp>;
15+
hold-while-undecided;
16+
};
17+
};
18+
19+
keymap {
20+
compatible = "zmk,keymap";
21+
label ="Default keymap";
22+
23+
default_layer {
24+
bindings = <
25+
&ht_bal LEFT_SHIFT A &ht_bal LEFT_CONTROL B
26+
&kp D &kp RIGHT_CONTROL>;
27+
};
28+
};
29+
};
30+
31+
&kscan {
32+
events = <
33+
ZMK_MOCK_PRESS(0,0,150)
34+
ZMK_MOCK_RELEASE(0,0,10)
35+
>;
36+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
s/.*hid_listener_keycode/kp/p
2+
s/.*mo_keymap_binding/mo/p
3+
s/.*on_hold_tap_binding/ht_binding/p
4+
s/.*decide_hold_tap/ht_decide/p
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
ht_binding_pressed: 0 new undecided hold_tap
2+
ht_decide: 0 hold behavior pressed while undecided
3+
kp_pressed: usage_page 0x07 keycode 0xE1 implicit_mods 0x00 explicit_mods 0x00
4+
ht_decide: 0 decided tap (balanced decision moment key-up)
5+
kp_pressed: usage_page 0x07 keycode 0x04 implicit_mods 0x00 explicit_mods 0x00
6+
kp_released: usage_page 0x07 keycode 0x04 implicit_mods 0x00 explicit_mods 0x00
7+
kp_released: usage_page 0x07 keycode 0xE1 implicit_mods 0x00 explicit_mods 0x00
8+
ht_binding_released: 0 cleaning up hold-tap
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <dt-bindings/zmk/keys.h>
2+
#include <behaviors.dtsi>
3+
#include <dt-bindings/zmk/kscan_mock.h>
4+
5+
/ {
6+
behaviors {
7+
ht_bal: behavior_hold_tap_balanced {
8+
compatible = "zmk,behavior-hold-tap";
9+
label = "HOLD_TAP_BALANCED";
10+
#binding-cells = <2>;
11+
flavor = "balanced";
12+
tapping-term-ms = <100>;
13+
quick-tap-ms = <300>;
14+
bindings = <&kp>, <&kp>;
15+
hold-while-undecided;
16+
hold-while-undecided-linger;
17+
};
18+
};
19+
20+
keymap {
21+
compatible = "zmk,keymap";
22+
label ="Default keymap";
23+
24+
default_layer {
25+
bindings = <
26+
&ht_bal LEFT_SHIFT A &ht_bal LEFT_CONTROL B
27+
&kp D &kp RIGHT_CONTROL>;
28+
};
29+
};
30+
};
31+
32+
&kscan {
33+
events = <
34+
ZMK_MOCK_PRESS(0,0,10)
35+
ZMK_MOCK_RELEASE(0,0,10)
36+
>;
37+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
s/.*hid_listener_keycode/kp/p
2+
s/.*mo_keymap_binding/mo/p
3+
s/.*on_hold_tap_binding/ht_binding/p
4+
s/.*decide_hold_tap/ht_decide/p
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
ht_binding_pressed: 0 new undecided hold_tap
2+
ht_decide: 0 hold behavior pressed while undecided
3+
kp_pressed: usage_page 0x07 keycode 0xE1 implicit_mods 0x00 explicit_mods 0x00
4+
ht_decide: 0 decided tap (balanced decision moment key-up)
5+
kp_pressed: usage_page 0x07 keycode 0xE1 implicit_mods 0x00 explicit_mods 0x00
6+
kp_released: usage_page 0x07 keycode 0xE1 implicit_mods 0x00 explicit_mods 0x00
7+
ht_binding_released: 0 cleaning up hold-tap
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <dt-bindings/zmk/keys.h>
2+
#include <behaviors.dtsi>
3+
#include <dt-bindings/zmk/kscan_mock.h>
4+
5+
/ {
6+
behaviors {
7+
ht_bal: behavior_hold_tap_balanced {
8+
compatible = "zmk,behavior-hold-tap";
9+
label = "HOLD_TAP_BALANCED";
10+
#binding-cells = <2>;
11+
flavor = "balanced";
12+
tapping-term-ms = <100>;
13+
quick-tap-ms = <200>;
14+
bindings = <&kp>, <&sk>;
15+
hold-while-undecided;
16+
hold-while-undecided-linger;
17+
};
18+
};
19+
20+
keymap {
21+
compatible = "zmk,keymap";
22+
label ="Default keymap";
23+
24+
default_layer {
25+
bindings = <
26+
&ht_bal LEFT_SHIFT LEFT_SHIFT &ht_bal LEFT_SHIFT LEFT_CONTROL
27+
&kp D &kp RIGHT_CONTROL>;
28+
};
29+
};
30+
};
31+
32+
&kscan {
33+
events = <
34+
ZMK_MOCK_PRESS(0,0,20)
35+
ZMK_MOCK_RELEASE(0,0,20)
36+
>;
37+
};

0 commit comments

Comments
 (0)