Puck.js and HID Keyboards

Bluetooth LE HID (Human Interface Devices) are things like Keyboards, Mice, and buttons. Puck.js can emulate these, so can simulate keys being pressed.

Note: Puck.js's 1v89 firmware doesn't support Bonding (needed by Windows HID devices). Pucks can work as keyboards on Android, Mac OS and Chromebook but until bonding is added they won't work on Windows.

BLE HID can be enabled by providing a HID Report to NRF.setServices, however we've provided common types of HID report in modules to make it easier:

Keyboards

Keyboard support is from the ble_hid_keyboard (About Modules) module.

var kb = require("ble_hid_keyboard");
NRF.setServices(undefined, { hid : kb.report });
// Send 'a'
kb.tap(kb.KEY.A, 0, function() {
  // Followed by capital 'A'
  kb.tap(kb.KEY.A, kb.MODIFY.SHIFT);
});

Multimedia Keys

However, the multimedia keys on keyboards (play/stop/volume/etc) are treated as a completely separate device by USB and BLE.

Support for this is from the ble_hid_controls (About Modules) module.

var controls = require("ble_hid_controls");
NRF.setServices(undefined, { hid : controls.report });

// Play/stop music
controls.playpause();

// Send 'volume up' twice
controls.volumeUp(function() {
  controls.volumeUp();
});

// Other options:
//controls.next();
//controls.prev();
//controls.stop();
//controls.mute();
//controls.volumeDown();

Low Level control

You can emulate a wide variety of other devices by providing your own HID report:

report = new Uint8Array([
  ]);
NRF.setServices(undefined, { hid : report });

You can then call NRF.sendHIDReport to send data. For Keyboards it must be an array of the form:

[modifier, reserved, key1, key2, key3, key4, key5, key6 ]

You can easily look up keyboard key codes, but for example to send the 'a' key, send [0,0,4,0,0,0,0,0]. To release it, send [0,0,0,0,0,0,0,0]

Note: The modifiers are defined in require("ble_hid_keyboard").MODIFY and common keys are in require("ble_hid_keyboard").KEY

The modifiers are as follows:

1   : left control
2   : left shift
4   : left alt
8   : left GUI
16  : right control
32  : right shift
64  : right alt
128 : right GUI

So to send capital A, send [2,0,4,0,0,0,0,0] followed by [0,0,0,0,0,0,0,0].

NRF.sendHIDReport([2,0,4], function() {
  NRF.sendHIDReport([0,0,0])
})

You can find key codes at http://www.usb.org/developers/hidpage/Hut1_12v2.pdf under the Keyboard/Keypad page, but for quick reference:

  • a...z are 4..26
  • 1..9 are 30..38
  • 0 is 39
  • Return is 40
  • Space is 44

This page is auto-generated from GitHub. If you see any mistakes or have suggestions, please let us know.