One of the more advanced features of the Circuit Playground is its ability to emulate a USB keyboard and mouse. To do this, we use the Arduino Mouse and Keyboard libraries.

The following sketch uses these libraries to emulate a USB keyboard and mouse and send key and mouse events when the pads are touched.

////////////////////////////////////////////////////////////////////////////
// Circuit Playground Capacitive Touch Tones
//
// Send keyboard and mouse events for each touch pad.
// https://www.arduino.cc/en/Reference/MouseKeyboard
//
// Author: Carter Nelson
// MIT License (https://opensource.org/licenses/MIT)

#include <Adafruit_CircuitPlayground.h>
#include <Mouse.h>
#include <Keyboard.h>

#define CAP_THRESHOLD   50
#define DEBOUNCE        250

uint8_t pads[] = {3, 2, 0, 1, 12, 6, 9, 10};
uint8_t numberOfPads = sizeof(pads)/sizeof(uint8_t);

boolean emulatorActive = false;

////////////////////////////////////////////////////////////////////////////
void takeAction(uint8_t pad) {
  Serial.print("PAD "); Serial.println(pad);
  switch (pad) {
    case 3:
      sendKey(KEY_LEFT_ARROW);
      break;
    case 2:
      sendKey(KEY_UP_ARROW);
      break;
    case 0:
      sendKey(KEY_DOWN_ARROW);
      break;
    case 1:
      sendKey(KEY_RIGHT_ARROW);
      break;
    case 12:
      sendKey(' ');
      break;
    case 6:
      sendMouse(MOUSE_LEFT);
      break;
    case 9:
      sendMouse(MOUSE_MIDDLE);
      break;
    case 10:
      sendMouse(MOUSE_RIGHT);
      break;
    default:
      Serial.println("THIS SHOULD NEVER HAPPEN.");
  }
}

////////////////////////////////////////////////////////////////////////////
boolean capButton(uint8_t pad) {
  // Check if capacitive touch exceeds threshold.
  if (CircuitPlayground.readCap(pad) > CAP_THRESHOLD) {
    return true;  
  } else {
    return false;
  }
}

////////////////////////////////////////////////////////////////////////////
void sendKey(char key) {
  Keyboard.press(key);
  Keyboard.releaseAll();
}

////////////////////////////////////////////////////////////////////////////
void sendMouse(char key) {
  Mouse.click(key);
}

////////////////////////////////////////////////////////////////////////////
void setup() {
  // Initialize serial.
  Serial.begin(9600); 
  
  // Initialize Circuit Playground library.
  CircuitPlayground.begin();
}

////////////////////////////////////////////////////////////////////////////
void loop() {
  // Indicate emulator status on red LED.
  CircuitPlayground.redLED(emulatorActive);

  // Check slide switch.
  if (!CircuitPlayground.slideSwitch()) {

    //-----------| EMULATOR OFF |-------------
    if (emulatorActive) {
      Keyboard.end();
      Mouse.end();
      emulatorActive = false;
    }
    
  } else {

    //-----------| EMULATOR ON |-------------
    if (!emulatorActive) {
      Keyboard.begin();
      Mouse.begin();
      emulatorActive = true;
    } 
 
    // Loop over every pad.
    for (int i=0; i<numberOfPads; i++) {
      
      // Check if pad is touched.
      if (capButton(pads[i])) {
        
        // Do something.
        takeAction(pads[i]);
        
        // But not too often.
        delay(DEBOUNCE);
      }
    }
  }
}

If this sounds familiar that's because this is essentially what a Makey Makey does. The default key mapping in the sketch provided above is similar to the original Makey Makey.

  • PAD #3: Left Arrow
  • PAD #2: Up Arrow
  • PAD #0: Down Arrow
  • PAD #1: Right Arrow
  • PAD #12: Space Bar
  • PAD #6: Left Mouse Button Click
  • PAD #9: Middle Mouse Button Click
  • PAD #10: Right Mouse Button Click

We can use alligator clips and some paper to create a cheat sheet for the key mapping. On a 6"x4" index card or other piece of paper, draw something similar to the picture below.

Now attach the alligator clips to the edge of the paper and the Circuit Playground as shown below.

To send the key press or mouse button click, touch the associated alligator clip.

READ THE FOLLOWING TO PREVENT UNEXPECTED BEHAVIOR.

Since there is a danger of flooding your computer with too many key presses or mouse clicks, the slide switch on the Circuit Playground is used to turn the keyboard/mouse emulation on and off. With the slide switch to the left (+), the emulator is active and the red LED will come on. The Circuit Playground will send key and mouse events with every capacitive touch. To turn the emulator off, set the slide switch to the right (-) position.

With this sketch loaded and running on the Circuit Playground, turn on the emulator using the slide switch. Then try out these Makey Makey apps:

This guide was first published on Nov 15, 2016. It was last updated on Nov 15, 2016.
This page (Cirkey Cirkey) was last updated on Oct 30, 2020.