From fe253ab0a337b661a1173731c9b2f9e2e909cdf5 Mon Sep 17 00:00:00 2001 From: Christian Skorup <93332319+Meisterian@users.noreply.github.com> Date: Sat, 28 Nov 2026 13:18:04 +0100 Subject: [PATCH] fix(hid): send correct boot protocol reports with adaptive NKRO After SET_PROTOCOL(boot), the adaptive NKRO send path kept sending its report-protocol report, which starts with report ID 0x01. A boot host (e.g. Nintendo Switch 2) reads byte 0 as the modifier mask, so every key arrived with Left Ctrl held. - endpoints.c: in boot protocol send the plain 8-byte boot report over USB and no NKRO report; over 2.4G drop only the NKRO report. - usb_hid.c: boot keyboard reports are exactly 8 bytes (the body type is sized for NKRO); skip the NKRO trimming in boot protocol, where byte 0 is the modifier mask and 0x04 means Left Alt. - hid_keychron.c: with adaptive NKRO build the boot report from the 6-key array even when the NKRO flag is set. Co-Authored-By: Claude Opus 5.5 --- app/include/zmk/usb_hid.h | 3 +++ app/src/endpoints.c | 17 +++++++++++++++++ app/src/hid_keychron.c | 3 ++- app/src/usb_hid.c | 16 ++++++++++++++-- 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/app/include/zmk/usb_hid.h b/app/include/zmk/usb_hid.h index c0cbc08a7ad..528a784f641 100644 --- a/app/include/zmk/usb_hid.h +++ b/app/include/zmk/usb_hid.h @@ -14,3 +14,6 @@ int zmk_usb_hid_send_consumer_report(void); int zmk_usb_hid_send_mouse_report(void); #endif // IS_ENABLED(CONFIG_ZMK_MOUSE) void zmk_usb_hid_set_protocol(uint8_t protocol); +#include +/* True when the host (or the 2.4G dongle's host) selected HID boot protocol. */ +bool zmk_usb_hid_is_boot_protocol(void); diff --git a/app/src/endpoints.c b/app/src/endpoints.c index 7821ade5f8d..fe8948b08de 100644 --- a/app/src/endpoints.c +++ b/app/src/endpoints.c @@ -180,6 +180,23 @@ int transport_send(struct zmk_hid_keyboard_report *report, uint8_t len) { static int send_keyboard_report(void) { extern struct zmk_adapative_nkro adapative_nkro; int ret = 0; + if (zmk_usb_hid_is_boot_protocol()) { + /* The host only understands 8-byte boot reports without report IDs, so it must not + * get the report-ID-prefixed 6KRO report or the NKRO bitmap report. */ + bool changed = kb_changed(); + changed |= nkro_changed(); + if (!changed) { + return 0; + } + if (current_instance.transport == ZMK_TRANSPORT_USB) { + return zmk_usb_hid_send_keyboard_report(); + } + /* Over 2.4G the 6KRO body is already in boot layout; only drop the NKRO report. */ + struct zmk_hid_keyboard_report *report = zmk_hid_get_keyboard_report(); + report->report_id = ZMK_HID_REPORT_ID_KEYBOARD; + report->body._reserved = 0; + return transport_send(report, CONFIG_ZMK_HID_KEYBOARD_REPORT_SIZE + 3); + } if (kb_changed()) { struct zmk_hid_keyboard_report *report = zmk_hid_get_keyboard_report(); report->report_id = ZMK_HID_REPORT_ID_KEYBOARD; diff --git a/app/src/hid_keychron.c b/app/src/hid_keychron.c index 7aa5a747e81..0dbe9d271a8 100644 --- a/app/src/hid_keychron.c +++ b/app/src/hid_keychron.c @@ -207,7 +207,8 @@ zmk_hid_boot_report_t *zmk_hid_get_boot_report(void) { return boot_report_rollover(keyboard_report.body.modifiers); } boot_report.modifiers = keyboard_report.body.modifiers; - if (!zmk_nkro_enable) { + /* With adaptive NKRO keyboard_report always holds the 6-key array, NKRO or not. */ + if (IS_ENABLED(CONFIG_ADAPATIVE_NKRO) || !zmk_nkro_enable) { #if CONFIG_ZMK_HID_KEYBOARD_REPORT_SIZE != HID_BOOT_KEY_LEN // Form a boot report from a report of different size. int out = 0; diff --git a/app/src/usb_hid.c b/app/src/usb_hid.c index a1cad844f41..92dd35ee769 100644 --- a/app/src/usb_hid.c +++ b/app/src/usb_hid.c @@ -46,11 +46,21 @@ static void set_proto_cb(const struct device *dev, uint8_t protocol) { void zmk_usb_hid_set_protocol(uint8_t protocol) { hid_protocol = protocol; } #endif /* IS_ENABLED(CONFIG_ZMK_USB_BOOT) */ +bool zmk_usb_hid_is_boot_protocol(void) { +#if IS_ENABLED(CONFIG_ZMK_USB_BOOT) + return hid_protocol == HID_PROTOCOL_BOOT; +#else + return false; +#endif +} + uint8_t *get_keyboard_report(size_t *len) { #if IS_ENABLED(CONFIG_ZMK_USB_BOOT) if (hid_protocol != HID_PROTOCOL_REPORT) { zmk_hid_boot_report_t *boot_report = zmk_hid_get_boot_report(); - *len = sizeof(*boot_report); + /* A boot keyboard report is exactly 8 bytes (modifiers, reserved, 6 keys). The + * report body type is larger than that because its key array is sized for NKRO. */ + *len = 2 + HID_BOOT_KEY_LEN; return (uint8_t *)boot_report; } #endif @@ -207,7 +217,9 @@ int zmk_usb_hid_send_report(const uint8_t *report, size_t len) { int err = 0; // LOG_HEXDUMP_ERR(report,len,"usb"); // cut reserve byte in nkro report; - if (report[0] == ZMK_HID_REPORT_ID_KEYBOARD_NKRO) { + /* Boot protocol reports have no report ID: byte 0 is the modifier mask, and + * 0x04 there means Left Alt, not the NKRO report ID. */ + if (!zmk_usb_hid_is_boot_protocol() && report[0] == ZMK_HID_REPORT_ID_KEYBOARD_NKRO) { uint8_t buffer[(ZMK_HID_KEYBOARD_NKRO_MAX_USAGE + 1) / 8 + 2]; buffer[0] = report[0]; buffer[1] = report[1];