This commit is contained in:
Nick Conway 2023-11-22 10:54:30 +08:00 committed by GitHub
commit 2630c5deea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
25 changed files with 295 additions and 3 deletions

View file

@ -14,3 +14,5 @@ properties:
tapping-term-ms: tapping-term-ms:
type: int type: int
default: 200 default: 200
trigger-each-binding:
type: boolean

View file

@ -27,6 +27,7 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
struct behavior_tap_dance_config { struct behavior_tap_dance_config {
uint32_t tapping_term_ms; uint32_t tapping_term_ms;
bool trigger_each_binding;
size_t behavior_count; size_t behavior_count;
struct zmk_behavior_binding *behaviors; struct zmk_behavior_binding *behaviors;
}; };
@ -143,6 +144,12 @@ static int on_tap_dance_binding_pressed(struct zmk_behavior_binding *binding,
// value, invoke the last binding available. // value, invoke the last binding available.
if (tap_dance->counter < cfg->behavior_count) { if (tap_dance->counter < cfg->behavior_count) {
tap_dance->counter++; tap_dance->counter++;
if (cfg->trigger_each_binding) {
behavior_keymap_binding_pressed(&tap_dance->config->behaviors[tap_dance->counter - 1],
event);
reset_timer(tap_dance, event);
return ZMK_BEHAVIOR_OPAQUE;
}
} }
if (tap_dance->counter == cfg->behavior_count) { if (tap_dance->counter == cfg->behavior_count) {
// LOG_DBG("Tap dance has been decided via maximum counter value"); // LOG_DBG("Tap dance has been decided via maximum counter value");
@ -162,6 +169,10 @@ static int on_tap_dance_binding_released(struct zmk_behavior_binding *binding,
return ZMK_BEHAVIOR_OPAQUE; return ZMK_BEHAVIOR_OPAQUE;
} }
tap_dance->is_pressed = false; tap_dance->is_pressed = false;
if (tap_dance->config->trigger_each_binding && !tap_dance->tap_dance_decided) {
behavior_keymap_binding_released(&tap_dance->config->behaviors[tap_dance->counter - 1],
event);
}
if (tap_dance->tap_dance_decided) { if (tap_dance->tap_dance_decided) {
release_tap_dance_behavior(tap_dance, event.timestamp); release_tap_dance_behavior(tap_dance, event.timestamp);
} }
@ -177,6 +188,13 @@ void behavior_tap_dance_timer_handler(struct k_work *item) {
return; return;
} }
LOG_DBG("Tap dance has been decided via timer. Counter reached: %d", tap_dance->counter); LOG_DBG("Tap dance has been decided via timer. Counter reached: %d", tap_dance->counter);
if (tap_dance->config->trigger_each_binding) {
if (!tap_dance->is_pressed) {
clear_tap_dance(tap_dance);
}
tap_dance->tap_dance_decided = true;
return;
}
press_tap_dance_behavior(tap_dance, tap_dance->release_at); press_tap_dance_behavior(tap_dance, tap_dance->release_at);
if (tap_dance->is_pressed) { if (tap_dance->is_pressed) {
return; return;
@ -214,9 +232,15 @@ static int tap_dance_position_state_changed_listener(const zmk_event_t *eh) {
stop_timer(tap_dance); stop_timer(tap_dance);
LOG_DBG("Tap dance interrupted, activating tap-dance at %d", tap_dance->position); LOG_DBG("Tap dance interrupted, activating tap-dance at %d", tap_dance->position);
if (!tap_dance->tap_dance_decided) { if (!tap_dance->tap_dance_decided) {
if (!tap_dance->config->trigger_each_binding) {
press_tap_dance_behavior(tap_dance, ev->timestamp); press_tap_dance_behavior(tap_dance, ev->timestamp);
}
if (!tap_dance->is_pressed) { if (!tap_dance->is_pressed) {
if (!tap_dance->config->trigger_each_binding) {
release_tap_dance_behavior(tap_dance, ev->timestamp); release_tap_dance_behavior(tap_dance, ev->timestamp);
} else {
clear_tap_dance(tap_dance);
}
} }
return ZMK_EV_EVENT_BUBBLE; return ZMK_EV_EVENT_BUBBLE;
} }
@ -248,6 +272,7 @@ static int behavior_tap_dance_init(const struct device *dev) {
TRANSFORMED_BINDINGS(n); \ TRANSFORMED_BINDINGS(n); \
static struct behavior_tap_dance_config behavior_tap_dance_config_##n = { \ static struct behavior_tap_dance_config behavior_tap_dance_config_##n = { \
.tapping_term_ms = DT_INST_PROP(n, tapping_term_ms), \ .tapping_term_ms = DT_INST_PROP(n, tapping_term_ms), \
.trigger_each_binding = DT_INST_PROP(n, trigger_each_binding), \
.behaviors = behavior_tap_dance_config_##n##_bindings, \ .behaviors = behavior_tap_dance_config_##n##_bindings, \
.behavior_count = DT_INST_PROP_LEN(n, bindings)}; \ .behavior_count = DT_INST_PROP_LEN(n, bindings)}; \
DEVICE_DT_INST_DEFINE(n, behavior_tap_dance_init, NULL, NULL, &behavior_tap_dance_config_##n, \ DEVICE_DT_INST_DEFINE(n, behavior_tap_dance_init, NULL, NULL, &behavior_tap_dance_config_##n, \

View file

@ -0,0 +1,2 @@
s/.*hid_listener_keycode/kp/p
s/.*on_tap_dance_binding/td_binding/p

View file

@ -0,0 +1,13 @@
td_binding_pressed: 2 created new tap dance
td_binding_pressed: 2 tap dance pressed
kp_pressed: usage_page 0x07 keycode 0x1E implicit_mods 0x00 explicit_mods 0x00
td_binding_released: 2 tap dance keybind released
kp_released: usage_page 0x07 keycode 0x1E implicit_mods 0x00 explicit_mods 0x00
td_binding_pressed: 2 tap dance pressed
kp_pressed: usage_page 0x07 keycode 0x1F implicit_mods 0x00 explicit_mods 0x00
td_binding_released: 2 tap dance keybind released
kp_released: usage_page 0x07 keycode 0x1F implicit_mods 0x00 explicit_mods 0x00
td_binding_pressed: 2 tap dance pressed
kp_pressed: usage_page 0x07 keycode 0x20 implicit_mods 0x00 explicit_mods 0x00
td_binding_released: 2 tap dance keybind released
kp_released: usage_page 0x07 keycode 0x20 implicit_mods 0x00 explicit_mods 0x00

View file

@ -0,0 +1,15 @@
#include <dt-bindings/zmk/keys.h>
#include <behaviors.dtsi>
#include <dt-bindings/zmk/kscan_mock.h>
#include "../behavior_keymap_each.dtsi"
&kscan {
events = <
ZMK_MOCK_PRESS(1,0,10)
ZMK_MOCK_RELEASE(1,0,10)
ZMK_MOCK_PRESS(1,0,10)
ZMK_MOCK_RELEASE(1,0,10)
ZMK_MOCK_PRESS(1,0,10)
ZMK_MOCK_RELEASE(1,0,10)
>;
};

View file

@ -0,0 +1,2 @@
s/.*hid_listener_keycode/kp/p
s/.*on_tap_dance_binding/td_binding/p

View file

@ -0,0 +1,9 @@
td_binding_pressed: 0 created new tap dance
td_binding_pressed: 0 tap dance pressed
td_binding_released: 0 tap dance keybind released
kp_pressed: usage_page 0x07 keycode 0x04 implicit_mods 0x00 explicit_mods 0x00
kp_released: usage_page 0x07 keycode 0x04 implicit_mods 0x00 explicit_mods 0x00
td_binding_pressed: 0 tap dance pressed
kp_pressed: usage_page 0x07 keycode 0xE2 implicit_mods 0x00 explicit_mods 0x00
td_binding_released: 0 tap dance keybind released
kp_released: usage_page 0x07 keycode 0xE2 implicit_mods 0x00 explicit_mods 0x00

View file

@ -0,0 +1,13 @@
#include <dt-bindings/zmk/keys.h>
#include <behaviors.dtsi>
#include <dt-bindings/zmk/kscan_mock.h>
#include "../behavior_keymap_each.dtsi"
&kscan {
events = <
ZMK_MOCK_PRESS(0,0,10)
ZMK_MOCK_RELEASE(0,0,10)
ZMK_MOCK_PRESS(0,0,200)
ZMK_MOCK_RELEASE(0,0,10)
>;
};

View file

@ -0,0 +1,2 @@
s/.*hid_listener_keycode/kp/p
s/.*on_tap_dance_binding/td_binding/p

View file

@ -0,0 +1,13 @@
td_binding_pressed: 0 created new tap dance
td_binding_pressed: 0 tap dance pressed
td_binding_released: 0 tap dance keybind released
kp_pressed: usage_page 0x07 keycode 0x04 implicit_mods 0x00 explicit_mods 0x00
kp_released: usage_page 0x07 keycode 0x04 implicit_mods 0x00 explicit_mods 0x00
td_binding_pressed: 0 tap dance pressed
kp_pressed: usage_page 0x07 keycode 0xE2 implicit_mods 0x00 explicit_mods 0x00
td_binding_released: 0 tap dance keybind released
kp_released: usage_page 0x07 keycode 0xE2 implicit_mods 0x00 explicit_mods 0x00
td_binding_pressed: 0 tap dance pressed
td_binding_released: 0 tap dance keybind released
kp_pressed: usage_page 0x07 keycode 0x06 implicit_mods 0x00 explicit_mods 0x00
kp_released: usage_page 0x07 keycode 0x06 implicit_mods 0x00 explicit_mods 0x00

View file

@ -0,0 +1,15 @@
#include <dt-bindings/zmk/keys.h>
#include <behaviors.dtsi>
#include <dt-bindings/zmk/kscan_mock.h>
#include "../behavior_keymap_each.dtsi"
&kscan {
events = <
ZMK_MOCK_PRESS(0,0,10)
ZMK_MOCK_RELEASE(0,0,10)
ZMK_MOCK_PRESS(0,0,150)
ZMK_MOCK_RELEASE(0,0,10)
ZMK_MOCK_PRESS(0,0,10)
ZMK_MOCK_RELEASE(0,0,10)
>;
};

View file

@ -0,0 +1,2 @@
s/.*hid_listener_keycode/kp/p
s/.*on_tap_dance_binding/td_binding/p

View file

@ -0,0 +1,10 @@
td_binding_pressed: 1 created new tap dance
td_binding_pressed: 1 tap dance pressed
kp_pressed: usage_page 0x07 keycode 0x16 implicit_mods 0x00 explicit_mods 0x00
td_binding_pressed: 2 created new tap dance
td_binding_pressed: 2 tap dance pressed
kp_pressed: usage_page 0x07 keycode 0x1E implicit_mods 0x00 explicit_mods 0x00
td_binding_released: 2 tap dance keybind released
kp_released: usage_page 0x07 keycode 0x1E implicit_mods 0x00 explicit_mods 0x00
td_binding_released: 1 tap dance keybind released
kp_released: usage_page 0x07 keycode 0x16 implicit_mods 0x00 explicit_mods 0x00

View file

@ -0,0 +1,13 @@
#include <dt-bindings/zmk/keys.h>
#include <behaviors.dtsi>
#include <dt-bindings/zmk/kscan_mock.h>
#include "../behavior_keymap_each.dtsi"
&kscan {
events = <
ZMK_MOCK_PRESS(0,1,10)
ZMK_MOCK_PRESS(1,0,10)
ZMK_MOCK_RELEASE(1,0,10)
ZMK_MOCK_RELEASE(0,1,10)
>;
};

View file

@ -0,0 +1,2 @@
s/.*hid_listener_keycode/kp/p
s/.*on_tap_dance_binding/td_binding/p

View file

@ -0,0 +1,10 @@
td_binding_pressed: 1 created new tap dance
td_binding_pressed: 1 tap dance pressed
kp_pressed: usage_page 0x07 keycode 0x16 implicit_mods 0x00 explicit_mods 0x00
td_binding_pressed: 2 created new tap dance
td_binding_pressed: 2 tap dance pressed
kp_pressed: usage_page 0x07 keycode 0x1E implicit_mods 0x00 explicit_mods 0x00
td_binding_released: 1 tap dance keybind released
kp_released: usage_page 0x07 keycode 0x16 implicit_mods 0x00 explicit_mods 0x00
td_binding_released: 2 tap dance keybind released
kp_released: usage_page 0x07 keycode 0x1E implicit_mods 0x00 explicit_mods 0x00

View file

@ -0,0 +1,13 @@
#include <dt-bindings/zmk/keys.h>
#include <behaviors.dtsi>
#include <dt-bindings/zmk/kscan_mock.h>
#include "../behavior_keymap_each.dtsi"
&kscan {
events = <
ZMK_MOCK_PRESS(0,1,10)
ZMK_MOCK_PRESS(1,0,10)
ZMK_MOCK_RELEASE(0,1,10)
ZMK_MOCK_RELEASE(1,0,10)
>;
};

View file

@ -0,0 +1,2 @@
s/.*hid_listener_keycode/kp/p
s/.*on_tap_dance_binding/td_binding/p

View file

@ -0,0 +1,10 @@
td_binding_pressed: 0 created new tap dance
td_binding_pressed: 0 tap dance pressed
kp_pressed: usage_page 0x07 keycode 0xE1 implicit_mods 0x00 explicit_mods 0x00
td_binding_pressed: 1 created new tap dance
td_binding_pressed: 1 tap dance pressed
kp_pressed: usage_page 0x07 keycode 0x16 implicit_mods 0x00 explicit_mods 0x00
td_binding_released: 1 tap dance keybind released
kp_released: usage_page 0x07 keycode 0x16 implicit_mods 0x00 explicit_mods 0x00
td_binding_released: 0 tap dance keybind released
kp_released: usage_page 0x07 keycode 0xE1 implicit_mods 0x00 explicit_mods 0x00

View file

@ -0,0 +1,13 @@
#include <dt-bindings/zmk/keys.h>
#include <behaviors.dtsi>
#include <dt-bindings/zmk/kscan_mock.h>
#include "../behavior_keymap_each.dtsi"
&kscan {
events = <
ZMK_MOCK_PRESS(0,0,400)
ZMK_MOCK_PRESS(0,1,10)
ZMK_MOCK_RELEASE(0,1,10)
ZMK_MOCK_RELEASE(0,0,10)
>;
};

View file

@ -0,0 +1,2 @@
s/.*hid_listener_keycode/kp/p
s/.*on_tap_dance_binding/td_binding/p

View file

@ -0,0 +1,20 @@
td_binding_pressed: 0 created new tap dance
td_binding_pressed: 0 tap dance pressed
td_binding_released: 0 tap dance keybind released
kp_pressed: usage_page 0x07 keycode 0x04 implicit_mods 0x00 explicit_mods 0x00
kp_released: usage_page 0x07 keycode 0x04 implicit_mods 0x00 explicit_mods 0x00
td_binding_pressed: 2 created new tap dance
td_binding_pressed: 2 tap dance pressed
kp_pressed: usage_page 0x07 keycode 0x1E implicit_mods 0x00 explicit_mods 0x00
td_binding_released: 2 tap dance keybind released
kp_released: usage_page 0x07 keycode 0x1E implicit_mods 0x00 explicit_mods 0x00
td_binding_pressed: 3 created new tap dance
td_binding_pressed: 3 tap dance pressed
kp_pressed: usage_page 0x07 keycode 0x04 implicit_mods 0x00 explicit_mods 0x00
td_binding_released: 3 tap dance keybind released
kp_released: usage_page 0x07 keycode 0x04 implicit_mods 0x00 explicit_mods 0x00
td_binding_pressed: 2 created new tap dance
td_binding_pressed: 2 tap dance pressed
kp_pressed: usage_page 0x07 keycode 0x1E implicit_mods 0x00 explicit_mods 0x00
td_binding_released: 2 tap dance keybind released
kp_released: usage_page 0x07 keycode 0x1E implicit_mods 0x00 explicit_mods 0x00

View file

@ -0,0 +1,17 @@
#include <dt-bindings/zmk/keys.h>
#include <behaviors.dtsi>
#include <dt-bindings/zmk/kscan_mock.h>
#include "../behavior_keymap_each.dtsi"
&kscan {
events = <
ZMK_MOCK_PRESS(0,0,10)
ZMK_MOCK_RELEASE(0,0,10)
ZMK_MOCK_PRESS(1,0,10)
ZMK_MOCK_RELEASE(1,0,10)
ZMK_MOCK_PRESS(1,1,10)
ZMK_MOCK_RELEASE(1,1,10)
ZMK_MOCK_PRESS(1,0,10)
ZMK_MOCK_RELEASE(1,0,200)
>;
};

View file

@ -0,0 +1,63 @@
#include <dt-bindings/zmk/keys.h>
#include <behaviors.dtsi>
#include <dt-bindings/zmk/kscan_mock.h>
/ {
behaviors {
ht: hold_tap {
compatible = "zmk,behavior-hold-tap";
label = "HOLD_TAP";
#binding-cells = <2>;
tapping-term-ms = <150>;
quick_tap_ms = <0>;
flavor = "tap-preferred";
bindings = <&kp>, <&kp>;
};
tdm: tap_dance_mixed {
compatible = "zmk,behavior-tap-dance";
label = "TAP_DANCE_MOD";
#binding-cells = <0>;
tapping-term-ms = <200>;
bindings = <&ht LSHIFT A>, <&ht LALT B>, <&ht LGUI C>;
trigger-each-binding;
};
tdb: tap_dance_basic {
compatible = "zmk,behavior-tap-dance";
label = "TAP_DANCE_BASIC";
#binding-cells = <0>;
tapping-term-ms = <200>;
bindings = <&kp N1>, <&kp N2>, <&kp N3>;
trigger-each-binding;
};
td2: tap_dance_basic_2 {
compatible = "zmk,behavior-tap-dance";
label = "TAP_DANCE_BASIC_2";
#binding-cells = <0>;
tapping-term-ms = <200>;
bindings = <&kp A>, <&kp B>, <&kp C>;
trigger-each-binding;
};
tds: tap_dance_single {
compatible = "zmk,behavior-tap-dance";
label = "TAP_DANCE_SINGlE";
#binding-cells = <0>;
tapping-term-ms = <200>;
bindings = <&kp S>;
};
};
keymap {
compatible = "zmk,keymap";
label ="Default keymap";
default_layer {
bindings = <
&tdm &tds
&tdb &td2>;
};
};
};

View file

@ -24,6 +24,10 @@ An array of one or more keybinds. This list can include [any ZMK keycode](../cod
The number of bindings in this array also determines the tap-dance's maximum number of keypresses. When a tap-dance reaches its maximum number of keypresses, it will immediately invoke the last behavior in its list of `bindings`, rather than waiting for [`tapping-term-ms`](#tapping-term-ms) to expire before the output is displayed. The number of bindings in this array also determines the tap-dance's maximum number of keypresses. When a tap-dance reaches its maximum number of keypresses, it will immediately invoke the last behavior in its list of `bindings`, rather than waiting for [`tapping-term-ms`](#tapping-term-ms) to expire before the output is displayed.
#### `trigger-each-binding`
If enabled, the tap-dance will output each behavior on key press. For example, a tap-dance with bindings `1`, `2`, and `3` will output 123 on triple tap.
### Example Usage ### Example Usage
<Tabs <Tabs