请教关于蓝牙键盘例程,如何获取CapsLock键的状态?

cxm007
Posts: 15
Joined: Wed Mar 15, 2023 2:44 am

请教关于蓝牙键盘例程,如何获取CapsLock键的状态?

Postby cxm007 » Tue Sep 12, 2023 9:40 am

环境:window11 vscode
版本库:v5.0-rc1*
芯片:ESP32-S3
:?:
ble_hid_device_demo.zip
(76.08 KiB) Downloaded 942 times
请问在这个例程中,连接window10电脑,要如何获取CapsLock、Numlock键的状态。就是当window上的 CapsLock、Numlock键状态改变时,芯片也可以及时获取到最新的CapsLock、Numlock状态。
如果能帮忙解决,非常感谢!
打破认知

cumind
Posts: 6
Joined: Tue Jul 11, 2023 2:49 am

Re: 请教关于蓝牙键盘例程,如何获取CapsLock键的状态?

Postby cumind » Thu Sep 14, 2023 2:05 am

Code: Select all

void tud_hid_set_report_cb(uint8_t instance, uint8_t report_id, hid_report_type_t report_type, uint8_t const *buffer, uint16_t bufsize)
{
    (void)report_id;

    // keyboard interface
    if (instance == ITF_NUM_KEYBOARD)
    {
        // Set keyboard LED e.g Capslock, Numlock etc...
        if (report_type == HID_REPORT_TYPE_OUTPUT)
        {
            // bufsize should be (at least) 1
            if (bufsize < 1)
                return;

            uint8_t const kbd_leds = buffer[0];
            // Cap_lock = 0;
            if (kbd_leds & KEYBOARD_LED_CAPSLOCK)
            {
                // Capslock On: disable blink, turn led on
                if (Cap_lock == false)
                    ESP_LOGI(TAG, "Capslock On");
                Cap_lock = true;
            }
            else
            {
                // Caplocks Off: back to normal blink
                if (Cap_lock == true)
                    ESP_LOGI(TAG, "Caplocks Off");
                Cap_lock = false;
            }

            if (kbd_leds & KEYBOARD_LED_NUMLOCK)
            {
                if (Num_Lock == false)
                    ESP_LOGI(TAG, "Numlock On");
                Num_Lock = true;
            }
            else
            {
                if (Num_Lock == true)
                    ESP_LOGI(TAG, "Numlock Off");
                Num_Lock = false;
            }
            if (kbd_leds & KEYBOARD_LED_SCROLLLOCK)
            {
                if (Scroll_Lock == false)
                    ESP_LOGI(TAG, "Scroll Lock On");
                Scroll_Lock = true;
            }
            else
            {
                if (Scroll_Lock == true)
                    ESP_LOGI(TAG, "Scroll Lock Off");
                Scroll_Lock = false;
            }

            if (kbd_leds & KEYBOARD_LED_COMPOSE)
            {
                if (Compos_Mode == false)
                    ESP_LOGI(TAG, "Composition Mode On");
                Compos_Mode = true;
            }
            else
            {
                if (Compos_Mode == true)
                    ESP_LOGI(TAG, "Composition Mode Off");
                Compos_Mode = false;
            }

            if (kbd_leds & KEYBOARD_LED_KANA)
            {
                if (kana_Mode == false)
                    ESP_LOGI(TAG, "Kana Mode On");
                kana_Mode = true;
            }
            else
            {
                if (kana_Mode == true)
                    ESP_LOGI(TAG, "Kana Mode Off");
                kana_Mode = false;
            }
        }
    }
}

cxm007
Posts: 15
Joined: Wed Mar 15, 2023 2:44 am

Re: 请教关于蓝牙键盘例程,如何获取CapsLock键的状态?

Postby cxm007 » Mon Sep 25, 2023 7:07 am

您好,非常感谢您的回答。不过,您提供的这个方法是usb keyboard的获取方式,而不是ble keyboard。
打破认知

Who is online

Users browsing this forum: No registered users and 107 guests