Skip to content

Commit

Permalink
mode_change: [fix] Keyboard event trigger from pressed_num to chage_num
Browse files Browse the repository at this point in the history
With changed event trigger, it is enable to ignore previously pressed keycodes like usual keyboard

Issue and comment link
 -> espressif/esp-iot-solution#370
  • Loading branch information
Junanjunan committed Jun 12, 2024
1 parent 54e1312 commit 6375bf4
Showing 1 changed file with 29 additions and 27 deletions.
56 changes: 29 additions & 27 deletions examples/mode_change/main/src/hid/hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void keyboard_cb(keyboard_btn_handle_t kbd_handle, keyboard_btn_report_t kbd_rep
uint8_t key[6] = {keycode};
uint8_t espnow_release_key [] = "0";
uint8_t modifier = 0;
if (kbd_report.key_pressed_num == 0) {
if (kbd_report.key_change_num < 0) {
if (current_mode == MODE_USB)
{
tud_hid_keyboard_report(HID_ITF_PROTOCOL_KEYBOARD, 0, key);
Expand All @@ -79,32 +79,34 @@ void keyboard_cb(keyboard_btn_handle_t kbd_handle, keyboard_btn_report_t kbd_rep
return;
}

for (int i = 0; i < kbd_report.key_pressed_num; i++) {
keycode = keycodes[kbd_report.key_data[i].output_index][kbd_report.key_data[i].input_index];
ESP_LOGI(__func__, "pressed_keycode: %x", keycode);
if (is_modifier(keycode)) {
modifier |= keycode;
keycode = 0;
}
uint8_t key[6] = {keycode};

// Convert keycode to uint8_t array for esp_now_send
char temp [6];
uint8_t converted_data [6];
sprintf(temp, "%d", keycode);
memcpy(converted_data, temp, sizeof(temp));

if (current_mode == MODE_USB)
{
tud_hid_keyboard_report(HID_ITF_PROTOCOL_KEYBOARD, modifier, key);
}
else if (current_mode == MODE_BLE)
{
esp_hidd_send_keyboard_value(hid_conn_id, modifier, &keycode, 1);
}
else if (current_mode == MODE_WIRELESS)
{
esp_now_send(peer_mac, converted_data, 32);
if (kbd_report.key_change_num > 0) {
for (int i = 0; i < kbd_report.key_pressed_num; i++) {
keycode = keycodes[kbd_report.key_data[i].output_index][kbd_report.key_data[i].input_index];
ESP_LOGI(__func__, "pressed_keycode: %x", keycode);
if (is_modifier(keycode)) {
modifier |= keycode;
keycode = 0;
}
uint8_t key[6] = {keycode};

// Convert keycode to uint8_t array for esp_now_send
char temp [6];
uint8_t converted_data [6];
sprintf(temp, "%d", keycode);
memcpy(converted_data, temp, sizeof(temp));

if (current_mode == MODE_USB)
{
tud_hid_keyboard_report(HID_ITF_PROTOCOL_KEYBOARD, modifier, key);
}
else if (current_mode == MODE_BLE)
{
esp_hidd_send_keyboard_value(hid_conn_id, modifier, &keycode, 1);
}
else if (current_mode == MODE_WIRELESS)
{
esp_now_send(peer_mac, converted_data, 32);
}
}
}
}
Expand Down

0 comments on commit 6375bf4

Please sign in to comment.