Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
#include "USB.h"
#include "USBHIDMouse.h"

#define HIDdisablePin 10
#define mouseXUp 1
#define mouseXDown 2
#define mouseYLeft 4
#define mouseYRight 5
#define click 7

USBHIDMouse Mouse;

void setup() {
  pinMode(HIDdisablePin, INPUT_PULLUP);
    // disable the mouse unless HIDdisablePin is connected to ground
  if (digitalRead(HIDdisablePin) == HIGH) {
    while (1) { /* infinite loop */
    }
  }
  // The rest of your code after this line
  pinMode(mouseXUp, INPUT_PULLUP);
  pinMode(mouseXDown, INPUT_PULLUP);
  pinMode(mouseYLeft, INPUT_PULLUP);
  pinMode(mouseYRight, INPUT_PULLUP);
  pinMode(click, INPUT_PULLUP);
  // initialize mouse control:
  Mouse.begin();
  USB.begin();
}

void loop() {

  if (digitalRead(mouseXUp) == LOW) {
    // move mouse up
    Mouse.move(0, -10);
  } else if (digitalRead(mouseXDown) == LOW) {
    // move mouse down
    Mouse.move(0, 10);
  }
  if (digitalRead(mouseYLeft) == LOW) {
    // move mouse left
    Mouse.move(-10, 0);
  } else if (digitalRead(mouseYRight) == LOW) {
    // move mouse right
    Mouse.move(10, 0);
  }

    // if the mouse button is pressed:
  if (digitalRead(click) == LOW) {
    // if the mouse is not pressed, press it:
    if (!Mouse.isPressed(MOUSE_LEFT)) {
      Mouse.press(MOUSE_LEFT);
    }
  } else {
    // if the mouse is pressed, release it:
    if (Mouse.isPressed(MOUSE_LEFT)) {
      Mouse.release(MOUSE_LEFT);
    }
  }
  delay(10);
}

HID Keyboard

Here is an example writing messages and using shortcuts with the HID keyboard protocal. You can find the full list of keys in the library source code. Again, be sure the pin 10 is connected to GND.

Code Block
#include "USB.h"
#include "USBHIDKeyboard.h"

#define HIDdisablePin 10
#define message 1
#define shortcut 2

USBHIDKeyboard Keyboard;

void setup() {
  pinMode(HIDdisablePin, INPUT_PULLUP);
  delay(20);
  // disable the mouse unless HIDdisablePin is connected to ground
  if (digitalRead(HIDdisablePin) == HIGH) {
    while (1) { /* infinite loop */
    }
  }
  // The rest of your code after this line

  pinMode(message, INPUT_PULLUP);
  pinMode(shortcut, INPUT_PULLUP);
  // initialize keyboard control:
   Keyboard.begin(KeyboardLayout_de_DE); // optional keyboard region setting
  USB.begin();
}

void loop() {
  

  if (digitalRead(message) == LOW) {
    // write a keyboard message
       Keyboard.print("Interaction Design ZHdK!");
      delay(400);
  } 
  if (digitalRead(shortcut) == LOW) {
    // focus on a random application window in OSX
      Keyboard.press(KEY_RIGHT_GUI);
      Keyboard.press(KEY_TAB);
      delay(100);
      Keyboard.release(KEY_TAB);
      delay(100);
      int randomCount = random(6);
      int count = 0;
      while (count < randomCount) {
        count++; 
      Keyboard.press(KEY_TAB);
      Keyboard.release(KEY_TAB);
      delay(400);
      }
      Keyboard.releaseAll();
  } 

  delay(10);
}