feature(modifiers): add explicit modifiers

this makes LS(LEFT_CONTROL) work as if shift and control were both
pressed explicitly. Previously, the left shift would have been released
as soon as another key was pressed. The implicit behavior is useful in
case of LS(NUMBER_1) when rolling over to other keys.

Also see #361.
This commit is contained in:
Okke Formsma
2021-01-22 17:00:19 +01:00
committed by Pete Johanson
parent 0c30b49063
commit 7b7701ae90
83 changed files with 425 additions and 336 deletions

View File

@@ -51,6 +51,24 @@ int zmk_hid_unregister_mod(zmk_mod_t modifier) {
return 0;
}
int zmk_hid_register_mods(zmk_mod_flags_t modifiers) {
for (zmk_mod_t i = 0; i < 8; i++) {
if (modifiers & (1 << i)) {
zmk_hid_register_mod(i);
}
}
return 0;
}
int zmk_hid_unregister_mods(zmk_mod_flags_t modifiers) {
for (zmk_mod_t i = 0; i < 8; i++) {
if (modifiers & (1 << i)) {
zmk_hid_unregister_mod(i);
}
}
return 0;
}
#define TOGGLE_KEYBOARD(match, val) \
for (int idx = 0; idx < ZMK_HID_KEYBOARD_NKRO_SIZE; idx++) { \
if (keyboard_report.body.keys[idx] != match) { \

View File

@@ -16,10 +16,10 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <dt-bindings/zmk/hid_usage_pages.h>
#include <zmk/endpoints.h>
static int hid_listener_keycode_pressed(const struct keycode_state_changed *ev) {
static int hid_listener_keycode_pressed(const struct zmk_keycode_state_changed *ev) {
int err;
LOG_DBG("usage_page 0x%02X keycode 0x%02X mods 0x%02X", ev->usage_page, ev->keycode,
ev->implicit_modifiers);
LOG_DBG("usage_page 0x%02X keycode 0x%02X implicit_mods 0x%02X explicit_mods 0x%02X",
ev->usage_page, ev->keycode, ev->implicit_modifiers, ev->explicit_modifiers);
switch (ev->usage_page) {
case HID_USAGE_KEY:
err = zmk_hid_keyboard_press(ev->keycode);
@@ -36,14 +36,15 @@ static int hid_listener_keycode_pressed(const struct keycode_state_changed *ev)
}
break;
}
zmk_hid_register_mods(ev->explicit_modifiers);
zmk_hid_implicit_modifiers_press(ev->implicit_modifiers);
return zmk_endpoints_send_report(ev->usage_page);
}
static int hid_listener_keycode_released(const struct keycode_state_changed *ev) {
static int hid_listener_keycode_released(const struct zmk_keycode_state_changed *ev) {
int err;
LOG_DBG("usage_page 0x%02X keycode 0x%02X mods 0x%02X", ev->usage_page, ev->keycode,
ev->implicit_modifiers);
LOG_DBG("usage_page 0x%02X keycode 0x%02X implicit_mods 0x%02X explicit_mods 0x%02X",
ev->usage_page, ev->keycode, ev->implicit_modifiers, ev->explicit_modifiers);
switch (ev->usage_page) {
case HID_USAGE_KEY:
err = zmk_hid_keyboard_release(ev->keycode);
@@ -59,6 +60,7 @@ static int hid_listener_keycode_released(const struct keycode_state_changed *ev)
return err;
}
}
zmk_hid_unregister_mods(ev->explicit_modifiers);
// There is a minor issue with this code.
// If LC(A) is pressed, then LS(B), then LC(A) is released, the shift for B will be released
// prematurely. This causes if LS(B) to repeat like Bbbbbbbb when pressed for a long time.