From f85f48837d7cd044c3b5e3c6df3c5572837f92cb Mon Sep 17 00:00:00 2001 From: Shayan Gohar Date: Tue, 4 Aug 2026 00:47:35 -0700 Subject: [PATCH] Add wireless battery reporting to Keychron raw HID --- docs/features/rawhid.md | 5 +++++ keyboards/keychron/common/keychron_raw_hid.c | 7 +++++++ keyboards/keychron/common/keychron_raw_hid.h | 1 + keyboards/keychron/common/wireless/wireless.c | 8 ++------ 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/docs/features/rawhid.md b/docs/features/rawhid.md index c3ecfbe099dc..3f728dd7b0f3 100644 --- a/docs/features/rawhid.md +++ b/docs/features/rawhid.md @@ -12,6 +12,11 @@ Add the following to your `rules.mk`: RAW_ENABLE = yes ``` +Keychron wireless keyboards can route raw HID reports received over the wireless +transport through the normal `raw_hid_receive()` handler by defining +`WIRELESS_RAW_ENABLE`. Keychron's command `KC_GET_BATTERY_LEVEL` (`0xA4`) +returns the cached battery percentage in byte 1 of the response. + ## Basic Configuration {#basic-configuration} By default, the HID Usage Page and Usage ID for the Raw HID interface are `0xFF60` and `0x61`. However, they can be changed if necessary by adding the following to your `config.h`: diff --git a/keyboards/keychron/common/keychron_raw_hid.c b/keyboards/keychron/common/keychron_raw_hid.c index b3657af7de4e..34ebf076383d 100644 --- a/keyboards/keychron/common/keychron_raw_hid.c +++ b/keyboards/keychron/common/keychron_raw_hid.c @@ -33,6 +33,7 @@ # include "snap_click.h" #endif #if defined(LK_WIRELESS_ENABLE) || defined(KC_BLUETOOTH_ENABLE) +# include "battery.h" # include "wireless.h" # ifdef LK_WIRELESS_ENABLE # include "lkbt51.h" @@ -168,6 +169,12 @@ bool kc_raw_hid_rx(uint8_t src, uint8_t *data, uint8_t length) { data[2] = get_highest_layer(layer_state); break; +# if defined(LK_WIRELESS_ENABLE) || defined(KC_BLUETOOTH_ENABLE) + case KC_GET_BATTERY_LEVEL: + data[1] = battery_get_percentage(); + break; +# endif + case KC_MISC_CMD_GROUP: switch (data[1]) { case MISC_GET_PROTOCOL_VER: diff --git a/keyboards/keychron/common/keychron_raw_hid.h b/keyboards/keychron/common/keychron_raw_hid.h index e10f659954a6..0cf68a6f3d5f 100644 --- a/keyboards/keychron/common/keychron_raw_hid.h +++ b/keyboards/keychron/common/keychron_raw_hid.h @@ -25,6 +25,7 @@ enum { KC_GET_FIRMWARE_VERSION = 0xA1, KC_GET_SUPPORT_FEATURE = 0xA2, KC_GET_DEFAULT_LAYER = 0xA3, + KC_GET_BATTERY_LEVEL = 0xA4, KC_MISC_CMD_GROUP = 0xA7, KC_KEYCHRON_RGB = 0xA8, KC_ANALOG_MATRIX = 0xA9, diff --git a/keyboards/keychron/common/wireless/wireless.c b/keyboards/keychron/common/wireless/wireless.c index a018d253ec11..844609e35985 100644 --- a/keyboards/keychron/common/wireless/wireless.c +++ b/keyboards/keychron/common/wireless/wireless.c @@ -606,13 +606,9 @@ void wireless_event_task(void) { case EVT_CONECTION_INTERVAL: report_buffer_set_inverval(event.params.interval); break; -#if defined(RAW_ENABLE) && defined(WILRESS_RAW_ENABLE) +#if defined(RAW_ENABLE) && defined(WIRELESS_RAW_ENABLE) case EVT_RAW_HID: -# ifdef VIA_ENABLE - via_raw_hid_receive(RAW_HID_SRC_WIRELESS, event.params.raw_hid_data, 32); -# else - kc_raw_hid_rx(RAW_HID_SRC_WIRELESS, event.params.raw_hid_data, 32); -# endif + raw_hid_receive(RAW_HID_SRC_WIRELESS, event.params.raw_hid_data, 32); lpm_timer_reset(); break; #endif