Mbed BLE HID - Send Keyboard PgUp and PgDn

Dear Friends,

I am attempting to use a Nano 33 BLE to create a HID Keyboard. I only want to pass two keyboard scan codes to the bluetooth connected iPad: PgUp and PgDn. Using the example ble_shining_kb.ino sketch I can see how to send the normal set of characters (like those I am typing here). What I cannot find is how to send the keyboard scan code to PgUp and PgDn, or any function type keys like that. The function sendCharacter() is the only function I can find. Does this ability exist in this Mbed BLE HID library? Can I use the Nano 33 BLE for this purpose?

Thank you in advance.

Regards,

Bob

The USB key code for PgUp is 75 (0x4B) and for PgDn is 78 (0x4E). On a Leonardo, you would add 136 to those numbers so they aren't treated as ASCII. That would be 211 and 214. I don't know if the "add 136" trick works on the Nano 33 BLE.

Hello John,

Thank you for the feedback. Since the sendCharacter() function is the only one I see that allows me to send over BLE as HID, I am not able to make anything work that allows sending Arrow_Right, Arrow_Left. I realize that I wrote PgUp, PgDn but that was not actually what I need... but still the problem exists. I found the reference you used and used the codes for left and right arrows but nothings works so far.

I fear that I must create a custom HID so that I can send control keys. I really hope that is not the case because I feel that is beyond my current skill set.

Regards,

Bob

You need to use: RIGHT_ARROW and LEFT_ARROW. That's what is defined in the USBKeyboard.h file.

Hi John,

I have taken the ble_shining_kb example sketch from the Examples -> Mbed BLE HID and cut it back to a very simple example. It will send a 'B' then '.' then a LEFT_ARROW. The result should end up producing BBBBB..... but the result I get is 'B.M' repeating, so 'B.MB.MB.MB.M' and so on. So my interpretation is that LEFT_ARROW is being interpreted as 'M'.

Note: I am using IDE 2.0.0 and I had to reduce the version of Arduino Mbed OS Nano Boards by Arduino (using the Boards Manager) from the current v3.4.1 to v3.0.1 in order to get it to pair properly. I don't know why, but with the newer board profile it will not pair properly to my iOS or MacOS devices.

Also, I find the LEFT_ARROW declared in HIDKeyboardService.h and I don't exactly understand the declaration. I will include my code and the LEFT_ARROW code below.

First, my small example as mentioned above:

#include "Nano33BleHID.h"
#include "signal_utils.h"

unsigned long beginOfDelay = 0;

Nano33BleKeyboard bleKb("Shining Keyboard");

void setup()
{
  // General setup.

  // Initialize both BLE and the HID.
  bleKb.initialize();

  // Launch the event queue that will manage both BLE events and the loop. 
  // After this call the main thread will be halted.
  MbedBleHID_RunEventThread();
}

void loop()
{
  if (!bleKb.connected()) { 
    return;
  }
  if (millis()>beginOfDelay+1000) { 
    // Retrieve the HIDService to update.
    auto *kb = bleKb.hid();
    kb->sendCharacter('B');
    kb->sendCharacter('.');
    kb->sendCharacter(LEFT_ARROW);
    beginOfDelay = millis();
  }    
}

Second, the declaration of LEFT_ARROW. I included all of the code from the HIDKeyboardService.h in case you see something that will help.

#ifndef BLE_HID_KEYBOARD_SERVICE_H__
#define BLE_HID_KEYBOARD_SERVICE_H__

#if BLE_FEATURE_GATT_SERVER

#include "services/HIDService.h"

/* -------------------------------------------------------------------------- */

/* Ascii index of special function char. */
enum FunctionKeyChar_t {
    KEY_F1 = 128,       /* F1 key */
    KEY_F2,             /* F2 key */
    KEY_F3,             /* F3 key */
    KEY_F4,             /* F4 key */
    KEY_F5,             /* F5 key */
    KEY_F6,             /* F6 key */
    KEY_F7,             /* F7 key */
    KEY_F8,             /* F8 key */
    KEY_F9,             /* F9 key */
    KEY_F10,            /* F10 key */
    KEY_F11,            /* F11 key */
    KEY_F12,            /* F12 key */
 
    KEY_PRINT_SCREEN,   /* Print Screen key */
    KEY_SCROLL_LOCK,    /* Scroll lock */
    KEY_CAPS_LOCK,      /* caps lock */
    KEY_NUM_LOCK,       /* num lock */
    KEY_INSERT,         /* Insert key */
    KEY_HOME,           /* Home key */
    KEY_PAGE_UP,        /* Page Up key */
    KEY_PAGE_DOWN,      /* Page Down key */
 
    RIGHT_ARROW,        /* Right arrow */
    LEFT_ARROW,         /* Left arrow */
    DOWN_ARROW,         /* Down arrow */
    UP_ARROW,           /* Up arrow */
};

typedef uint16_t KeyCode_t;

/* Represent a key that could be sent by the HID. */
struct KeySym_t {
  enum Modifier {
    KEY_NONE  = 0,
    KEY_CTRL  = 1 << 0,
    KEY_SHIFT = 1 << 1,
    KEY_ALT   = 1 << 2,
  };

  /* Construct a Keysym directly by raw values. */
  KeySym_t(uint8_t _usage, uint8_t _modifiers) 
    : usage(_usage)
    , modifiers(_modifiers)
  {}

  /* Construct a Keysym from a specific keycode. */
  explicit KeySym_t(KeyCode_t _keycode);

  uint8_t usage;
  uint8_t modifiers;
};

/**
 * BLE HID Keyboard Service
 *
 * @par usage
 *
 * When this class is instantiated, it adds a keyboard HID service in 
 * the GattServer.
 *
 * @attention Multiple instances of this hid service are not supported.
 * @see HIDService
 */
class HIDKeyboardService : public HIDService {
 public:
  HIDKeyboardService(BLE &_ble);

  ble::adv_data_appearance_t appearance() const override {
    return ble::adv_data_appearance_t::KEYBOARD;
  }

  /* Return the KeySym for a specific character. */
  KeySym_t charToKeySym(unsigned char c) const;

  /* Send a press & release report for a single character. */
  void sendCharacter(unsigned char c);

  /* Register a down report for the specific keysym. */
  void keydown(KeySym_t keysym);

  /* Register a release report. */
  void keyup();
};

/* -------------------------------------------------------------------------- */

#endif // BLE_FEATURE_GATT_SERVER

#endif // BLE_HID_KEYBOARD_SERVICE_H__

Thanks again.

Regards,

Bob

I struggle with a similar problem here.

I want my Nano 33 BLE to send keyboard shortcuts (ie, Shift+F3) to my Macbook via Bluetooth, on trigger. So far, I've succeeded in sending characters.

However, figuring out shortcuts is beyond my understanding. I've been playing with that Shining Keyboard example, too.

Same as @bobkelley22, I don't understand how keydown and keyup methods work in this Mbed BLE HID project.

I thought of transmitting shortcuts like Shift+F3, by sequentially sending Shift and F3 keys by their USBKeyboard.h definitions (KEY_SHIFT, KEY_F3), using 'sendCharacter' method. It doesn't work for me either. Moreover, I don't see how such sequences could be implemented in a standard Keyboard.h fashion ("press a, press b, delay 100, release all").

This Mbed BLE HID project was very useful for me in terms of turning my Arduino (readily-packed with Bluetooth chip) into a wireless tool. I would appreciate any help in solving this issue (or any clues on what other software may help me with shortcuts on 33 BLE).

Anyone else find a solution here? I'm looking to create a bluetooth "keyboard/mouse" remote that will send the following signals to an Android device after reading inputs from an 8-directional stick switch (with encoder & center-push function...specifically, an Alps Alpine RKJXW1014002):

  • Tab
  • Shift-Tab
  • Enter/Return/Select
  • Up
  • Down
  • Left
  • Right
  • Scroll Up
  • Scroll Down
  • Back
  • Home

Thanks for your time and insight!

The farthest I got with this library (thanks to the guys from alexgyver community) was to make it send at least something, except simple characters. The syntax is:

kb->keydown(KeySym_t(KEY_F5));
kb->SendReport();
kb->keyup();
kb->SendReport();

It doesn't make any sense however, as it sends ƒ character instead of pushing F5. It's better than nothing, I guess.

Hopefully, someone will help to shine some light on how this library works, or will create a better one.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.