Skip to content
Francis Gulotta edited this page Nov 28, 2015 · 29 revisions

The Keypad class constructs an object that represents a single Keypad attached to the board.

Supported Keypads:

Parameters

  • General Options

    Property Type Value/Description Default Required
    controller string AT42QT1070, MPR121, MPR121QR2, QTOUCH, VKEY, ANALOG. The Name of the controller to use ANALOG Yes
    keys array Mapping of key values By Device No
    holdtime Number Time in milliseconds that the button must be held until emitting a "hold" event. 500ms no
  • VKEY Options (controller: "VKEY")

    Property Type Value/Description Default Required
    pin number, string Analog pin Yes
  • ANALOG Options (controller: "ANALOG")

    Property Type Value/Description Default Required
    pin number, string Analog pin Yes
    length number Number of keys (required only if keys is not specified) Yes

Shape

Property Name Description Read Only
value Index of the pressed key No
target Mapped key value of pressed key No

Component Initialization

ANALOG

new five.Keypad({
  pin: "A0",
  length: 16
});

VKEY

new five.Keypad({
  controller: "VKEY",
  pin: "A0",
});

MPR121

new five.Keypad({
   controller: "MPR121"
});

MPR121QR2

new five.Keypad({
   controller: "MPR121QR2"
});

QTOUCH / AT42QT1070

new five.Keypad({
  controller: "QTOUCH", // or "AT42QT1070"
});

Usage

var five = require("johnny-five");
var board = new five.Board();

board.on("ready", function() {
  // MPR121 3x4 Capacitive Touch Pad
  var keypad = new five.Keypad({
    controller: "MPR121",
    keys: [
      ["!", "@", "#"],
      ["$", "%", "^"],
      ["&", "-", "+"],
      ["_", "=", ":"]
    ]
  });

  ["change", "press", "hold", "release"].forEach(function(eventType) {
    keypad.on(eventType, function(data) {
      console.log("Event: %s, Target: %s", eventType, data.which);
    });
  });
});

A Note About The keys Option

The keys option allows the assignment of any value to any key/button on the device. The value of keys may be a flat array of strings, or an array of arrays where the nested array are "rows" on the device. The values given in the keys array are the values that the instance will report for key/button presses.

Using the MPR121QR2 to illustrate keys:

The buttons are labelled 1, 2, 3, 4, 5, 6, 7, 8, 9. If the instance of Keypad is created without an explicit keys property, the output will be exactly those numbers; ie. pressing 1 will result in an event whose data object's which property is the value 1 (and so on):

var keypad = new five.Keypad({
  controller: "MPR121QR2"
});

keypad.on("press", function(event) {
  console.log("Which button?", event.which);
});

However, the keys property allows a program to assign any value to be the resulting value (because who knows—you may want to use symbols!)

var keypad = new five.Keypad({
  controller: "MPR121QR2",
  keys: [
    ["", "△", ""],
    ["◁", "☐", "▷"],
    ["", "▽", ""]
  ]
});

keypad.on("press", function(event) {
  if (event.which) {
    console.log(event.which);
  }
});

Pressing 2, 8, 4, 6, 5 would result in the following output:

$ node eg/keypad-symbol-MPR121QR2.js
1448040971577 Device(s) /dev/cu.usbmodem1411
1448040971586 Connected /dev/cu.usbmodem1411
1448040975171 Repl Initialized
>> △
▽
◁
▷
☐

If your program prefers a flat array, the equivalent program would be:

var keypad = new five.Keypad({
  controller: "MPR121QR2",
  keys: ["", "△", "", "◁", "☐", "▷", "", "▽", ""]
});

keypad.on("press", function(event) {
  if (event.which) {
    console.log(event.which);
  }
});

Events

  • change Emitted for hold, down, up

  • hold The key has been held for holdtime milliseconds

  • down, press The key has been pressed.

  • up, release The key has been released.

The following data object is emitted with each event:

Key Name Description
which Value of key pressed from keys array
timestamp Timestamp at time of key press

Examples

Clone this wiki locally