This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Sending multiple characters using single keystroke in HID keyboard example

Hi experts I am trying to play a little with BLE HID keyboard example supplied by nordic I see that there is an array of 5 characters "hello" + "enterkey press" which is sent when button 1 is pressed. On every key press first "h" is sent then "e" then "l" .... and so on . Last character being sent is enterkey press. Now i was wondering will it be possible to send complete word "hello+ enterkeypress" in single button press of button 1. if yes can you guide me as to what are the necessary changes in source code i have to make.

SDK being used is 12.2.0 Softdevice S130 Board nRF51-dk nrf51422

  • Hi,

    You can send up to 6 letters/keys in one HID transfer(Input Report). In the function bsp_event_handler() you can modify the BSP_EVENT_KEY_0 case like this to send the whole word in one report.

    case BSP_EVENT_KEY_0:
        if (m_conn_handle != BLE_CONN_HANDLE_INVALID)
        {
            
            keys_send(sizeof(m_sample_key_press_scan_str), m_sample_key_press_scan_str);
            
        }
        break;
    

    BUT, the USB-HID spec says it's not possible to send two of the same letter in one HID transfer. So if you test this on your phone, the phone will do a sanity check on the content and discard double-letters. So you will receive helo, instead of hello. A solution to this could therefore be to do a for-loop when the button is pressed, and call keys_send(1,...) for each letter in the word you want to send. (i.e. 1 HID transfer for each letter ). This could look something like this:

     case BSP_EVENT_KEY_0:
            if (m_conn_handle != BLE_CONN_HANDLE_INVALID)
            {
                  uint32_t i;
                
                  for(i = 0; i < sizeof(m_sample_key_press_scan_str); i++)
                  {
                      keys_send(1, p_key);
                      p_key++;
                  }
                  p_key = m_sample_key_press_scan_str;
            
            }
            break;
    
  • Dear Sigurd,

    I think your answer is partially wrong.

    Yes, an input report accepts up to 6 characters, but one needs to understand that this is supposed to be 6 characters which were pressed in continuous order, equal to 6 x key-press actions. HID keyboard stuff is different from older MFC/PS2 keyboard handling.

    In order to send a "character string" the software needs to send an input report for each key, followed by the release of this key.

    Sending "hello" with keys_send() would mean this on a real keyboard : you press and hold the keys h, e, l, l, o - then release h, e, l, l, o - which is of course not a valid way of typing.

    matthias

Related