Skip to content

Commit d71aed3

Browse files
committed
Smart-word: original PR (zmkfirmware#1451), updated to Zephyr-3.2
1 parent 4137e58 commit d71aed3

File tree

7 files changed

+112
-5
lines changed

7 files changed

+112
-5
lines changed

app/dts/behaviors/caps_word.dtsi

+15
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,22 @@
1212
compatible = "zmk,behavior-caps-word";
1313
label = "CAPS_WORD";
1414
#binding-cells = <0>;
15+
mods = <MOD_LSFT>;
1516
continue-list = <UNDERSCORE BACKSPACE DELETE>;
17+
ignore-alphas;
18+
ignore-numbers;
19+
ignore-modifiers;
20+
};
21+
};
22+
23+
behaviors {
24+
/omit-if-no-ref/ num_word: behavior_num_word {
25+
compatible = "zmk,behavior-caps-word";
26+
label = "NUM_WORD";
27+
#binding-cells = <0>;
28+
// layers = <xx>; // to be specified in user config using "&num_word { layers = <xx>; };"
29+
continue-list = <BACKSPACE DELETE DOT COMMA>;
30+
ignore-numbers;
1631
};
1732
};
1833
};

app/dts/bindings/behaviors/zmk,behavior-caps-word.yaml

+8
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,11 @@ properties:
1313
required: true
1414
mods:
1515
type: int
16+
layers:
17+
type: int
18+
ignore-alphas:
19+
type: boolean
20+
ignore-numbers:
21+
type: boolean
22+
ignore-modifiers:
23+
type: boolean

app/src/behaviors/behavior_caps_word.c

+26-5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ struct caps_word_continue_item {
3232

3333
struct behavior_caps_word_config {
3434
zmk_mod_flags_t mods;
35+
int8_t layers;
36+
bool ignore_alphas;
37+
bool ignore_numbers;
38+
bool ignore_modifiers;
3539
uint8_t index;
3640
uint8_t continuations_count;
3741
struct caps_word_continue_item continuations[];
@@ -44,12 +48,22 @@ struct behavior_caps_word_data {
4448
static void activate_caps_word(const struct device *dev) {
4549
struct behavior_caps_word_data *data = dev->data;
4650

51+
const struct behavior_caps_word_config *config = dev->config;
52+
53+
if (config->layers > -1) {
54+
zmk_keymap_layer_activate(config->layers);
55+
}
4756
data->active = true;
4857
}
4958

5059
static void deactivate_caps_word(const struct device *dev) {
5160
struct behavior_caps_word_data *data = dev->data;
5261

62+
const struct behavior_caps_word_config *config = dev->config;
63+
64+
if (config->layers > -1) {
65+
zmk_keymap_layer_deactivate(config->layers);
66+
}
5367
data->active = false;
5468
}
5569

@@ -120,8 +134,10 @@ static void caps_word_enhance_usage(const struct behavior_caps_word_config *conf
120134
return;
121135
}
122136

123-
LOG_DBG("Enhancing usage 0x%02X with modifiers: 0x%02X", ev->keycode, config->mods);
124-
ev->implicit_modifiers |= config->mods;
137+
if (config->mods != 0) {
138+
LOG_DBG("Enhancing usage 0x%02X with modifiers: 0x%02X", ev->keycode, config->mods);
139+
ev->implicit_modifiers |= config->mods;
140+
}
125141
}
126142

127143
static int caps_word_keycode_state_changed_listener(const zmk_event_t *eh) {
@@ -145,8 +161,9 @@ static int caps_word_keycode_state_changed_listener(const zmk_event_t *eh) {
145161

146162
caps_word_enhance_usage(config, ev);
147163

148-
if (!caps_word_is_alpha(ev->keycode) && !caps_word_is_numeric(ev->keycode) &&
149-
!is_mod(ev->usage_page, ev->keycode) &&
164+
if ((!caps_word_is_alpha(ev->keycode) || !config->ignore_alphas) &&
165+
(!caps_word_is_numeric(ev->keycode) || !config->ignore_numbers) &&
166+
(!is_mod(ev->usage_page, ev->keycode) || !config->ignore_modifiers) &&
150167
!caps_word_is_caps_includelist(config, ev->usage_page, ev->keycode,
151168
ev->implicit_modifiers)) {
152169
LOG_DBG("Deactivating caps_word for 0x%02X - 0x%02X", ev->usage_page, ev->keycode);
@@ -177,7 +194,11 @@ static int behavior_caps_word_init(const struct device *dev) {
177194
static struct behavior_caps_word_data behavior_caps_word_data_##n = {.active = false}; \
178195
static struct behavior_caps_word_config behavior_caps_word_config_##n = { \
179196
.index = n, \
180-
.mods = DT_INST_PROP_OR(n, mods, MOD_LSFT), \
197+
.mods = DT_INST_PROP_OR(n, mods, 0), \
198+
.layers = DT_INST_PROP_OR(n, layers, -1), \
199+
.ignore_alphas = DT_INST_PROP(n, ignore_alphas), \
200+
.ignore_numbers = DT_INST_PROP(n, ignore_numbers), \
201+
.ignore_modifiers = DT_INST_PROP(n, ignore_modifiers), \
181202
.continuations = {LISTIFY(DT_INST_PROP_LEN(n, continue_list), BREAK_ITEM, (, ), n)}, \
182203
.continuations_count = DT_INST_PROP_LEN(n, continue_list), \
183204
}; \
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
s/.*hid_listener_keycode_//p
2+
s/.*hid_implicit_modifiers_//p
3+
s/.*caps_word_enhance_usage/enhance_usage/p
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
enhance_usage: Enhancing usage 0x04 with modifiers: 0x02
2+
pressed: usage_page 0x07 keycode 0x04 implicit_mods 0x02 explicit_mods 0x00
3+
press: Modifiers set to 0x02
4+
released: usage_page 0x07 keycode 0x04 implicit_mods 0x00 explicit_mods 0x00
5+
release: Modifiers set to 0x00
6+
pressed: usage_page 0x07 keycode 0xE1 implicit_mods 0x00 explicit_mods 0x00
7+
press: Modifiers set to 0x02
8+
released: usage_page 0x07 keycode 0xE1 implicit_mods 0x00 explicit_mods 0x00
9+
release: Modifiers set to 0x00
10+
pressed: usage_page 0x07 keycode 0x04 implicit_mods 0x00 explicit_mods 0x00
11+
press: Modifiers set to 0x00
12+
released: usage_page 0x07 keycode 0x04 implicit_mods 0x00 explicit_mods 0x00
13+
release: Modifiers set to 0x00
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <dt-bindings/zmk/keys.h>
2+
#include <behaviors.dtsi>
3+
#include <dt-bindings/zmk/kscan_mock.h>
4+
5+
&caps_word {
6+
/delete-property/ ignore-modifiers;
7+
};
8+
9+
/ {
10+
keymap {
11+
compatible = "zmk,keymap";
12+
label = "Default keymap";
13+
14+
default_layer {
15+
bindings = <
16+
&caps_word &kp A
17+
&kp LSHFT &none
18+
>;
19+
};
20+
};
21+
};
22+
23+
24+
&kscan {
25+
events = <
26+
ZMK_MOCK_PRESS(0,0,10)
27+
ZMK_MOCK_RELEASE(0,0,10)
28+
ZMK_MOCK_PRESS(0,1,10)
29+
ZMK_MOCK_RELEASE(0,1,10)
30+
ZMK_MOCK_PRESS(1,0,10)
31+
ZMK_MOCK_RELEASE(1,0,10)
32+
ZMK_MOCK_PRESS(0,1,10)
33+
ZMK_MOCK_RELEASE(0,1,10)
34+
>;
35+
};

docs/docs/behaviors/caps-word.md

+12
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ By default, the caps word will remain active when any alphanumeric character or
3737
};
3838
```
3939

40+
#### Continue on modifiers
41+
42+
By default, the caps word will remain active when any modifiers are pressed. If you
43+
would like to deactivate caps word when modifiers are pressed, you can delete the
44+
`ignored-modifiers` property in your keymap:
45+
46+
```
47+
&caps_word {
48+
/delete-property/ ignore-modifiers;
49+
};
50+
```
51+
4052
#### Applied Modifier(s)
4153

4254
In addition, if you would like _multiple_ modifiers, instead of just `MOD_LSFT`, you can override the `mods` property:

0 commit comments

Comments
 (0)