ble_app_hids_keyboard - can the HID keyboard peripheral turn on Caps lock by itself?

Hi guys. May I ask one firmware code question for ble_app_hids_keyboard_pca10056_s113 project from 17.1.0 nRF5 SDK:

https://infocenter.nordicsemi.com/topic/sdk_nrf5_v17.1.0/ble_sdk_app_hids_keyboard.html

First, as the infocenter says, if the BLE central "writes" 02 to the Output Report characteristic, this simulates the Caps Lock ON.

When "writing" 02, is this a "write with response" or "write without response"? Since this Output Report characteristics has both, I'm curious which "write" was used.

Second, instead of using the Output Report characteristic, is there a way to change this example to turn on or off the Caps Lock?

EX: use button 4 to turn on or off the Caps Lock - is this possible? What should I change in order to do this?

  • Hello,

    All HID reports are sent using the function send_key_scan_press_release() -> ble_hids_inp_rep_send() -> sd_ble_gatts_hvx(), so it is neither "write with response" nor "write without response". It is a notification, since the peripheral is the server, not the client.

    Regarding your caps lock question. The example is by default set up to use the shift on button 2. This is why the letters are big if you hold button 2 and press button 1.

    If you want to use caps lock, however, this is a different type of key. It is an on/off key. Most keys in a hid report is just reporting whether they are pressed or released (which is the case for the caps lock as well), but the caps lock will toggle an internal state in whatever it is connected to. So the next time you send a character, it is the computer that knows that it should be an uppercase letter. What I am trying to say is that the keyboard doesn't send a different signal on "A" and "a". It is just the caps lock that has been toggled. 

    You may be confused by looking at this example, if you look at on_hid_rep_char_write. This means that the computer has turned on caps lock (from another keyboard connected). This state is reported to HID devices, so that they can turn on the Caps Lock LED (if it has one). The same goes for num lock and scroll lock.

    The way this handles key presses is, in my opinion overly complicated. Please see my colleagues reply in this ticket:

     how are keyboard key release events sent in example code? 

    However, you can still use it. Just know that whenever you send a button press, it will also send a release report for that same button immediately after (If it didn't, it would act as if you held down that button on the keyboard). 

    Also, the example should have used a file similar to SDK\components\libraries\usbd\class\hid\kbd\app_usbd_hid_kbd.h to show all the possible button presses. Instead it just uses hardcoded values for the letters that it is using. So I suggest you copy the content from this file (or include it in your project), so that you have more letters to pick from.

    In that list, you can see that the value for caps lock is:

    APP_USBD_HID_KBD_CAPS_LOCK       = 57, /**<KBD_CAPS_LOCK       code*/

    So if you send a key press (and release) with this value, it will toggle the caps lock.

    Best regards,

    Edvin

  • Thank you for your answer Edvin.

    d_ble_gatts_hvx(), so it is neither "write with response" nor "write without response". It is a notification, since the peripheral is the server, not the client.

    Sorry, but I was mentioning the HID output report: From the INFO CENTER, 6. Simulate Caps Lock ON,

    https://infocenter.nordicsemi.com/index.jsp?topic=%2Fsdk_nrf5_v17.1.0%2Fble_sdk_app_hids_keyboard.html

    Setting the modifier bit to 02 will simulate Caps Lock ON.
    In nRF Connect, select the HID Report, which has the properties Read, WriteWithoutResponse, and Write (the Output Report) and UUID: 0x2A4D.

    As explained here, I used two nRF52 boards and the nRF Connect for Desktop for "writing" that output report data to 02.

    When doing this, was this a "write with response" or "write without response"?

  • Matthew K said:
    When doing this, was this a "write with response" or "write without response"?

    Does it really matter? It is the central (computer/phone) that connects to the peripheral that has to decide whether it uses "write without response" or "write". 

    However, putting a breakpoint in on_write() in ble_hids.c I see that it triggers when I write 0x02, simulating caps on, and the p_ble_evt has evt_id 0x50 = BLE_GATTS_EVT_WRITE. This event covers both write with and without response (because the difference is handled in the softdevice, so you don't need to worry about the difference). 

    The p_ble_evt->gatts_evt.write.op = 0x02, which is BLE_GATTS_OP_WRITE_CMD, which means a write without response, as opposed to 0x01 = BLE_GATTS_OP_WRITE_REQ.

    So it is a write without response.

    But the real question, which I interpreted from your original question was how you can toggle caps lock from the nRF52 DK running the ble_gap_hids_keyboard example, right?

    This is usually not triggered from the computer itself, but from the keyboard (your DK). The use of simulating this caps lock on by typing 0x02 is the same as turning on caps lock from a different computer.

    Best regards,

    Edvin

  • Hi Edvin. Thanks for clarifying the write part. Back to your original answer,

    So if you send a key press (and release) with this value, it will toggle the caps lock.

    About sending APP_USBD_HID_KBD_CAPS_LOCK = 57, the Nordic Infocenter says

    https://infocenter.nordicsemi.com/index.jsp?topic=%2Fsdk_nrf5_v17.1.0%2Fble_sdk_app_hids_keyboard.html

    "The format used for keyboard reports is the following byte array:

    [modifier, reserved, Key1, Key2, Key3, Key4, Key6, Key7]"

    Should I set that caps lock 57 in the modifier or Key1?

  • Did you test? How about testing the same API that is used for sending the "hello" messages? Or look at how the shift key implementation is used in the "caps lock on" and "caps lock off" messages?

    BR;
    Edvin

Related