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

Parents
  • 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;
    
Reply
  • 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;
    
Children
No Data
Related