From dc21e1da8a20d666e0736b22b6bc881505e796e5 Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Mon, 24 Oct 2011 10:55:39 -0400 Subject: [PATCH] Added examples for the Keyboard library of the Leonardo --- .../KeyboardMessage/KeyboardMessage.ino | 43 +++++++++++++++++++ .../KeyboardSerial/KeyboardSerial.ino | 33 ++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 libraries/Keyboard/KeyboardMessage/KeyboardMessage.ino create mode 100644 libraries/Keyboard/KeyboardSerial/KeyboardSerial.ino diff --git a/libraries/Keyboard/KeyboardMessage/KeyboardMessage.ino b/libraries/Keyboard/KeyboardMessage/KeyboardMessage.ino new file mode 100644 index 000000000..51b1ce2c6 --- /dev/null +++ b/libraries/Keyboard/KeyboardMessage/KeyboardMessage.ino @@ -0,0 +1,43 @@ +/* + Keyboard Button test + + Sends a text string when a button is pressed. + + The circuit: + * pushbutton attached from pin 4 to +5V + * 10-kilohm resistor attached from pin 4 to ground + + created 24 Oct 2011 + by Tom Igoe + + This example code is in the public domain. + + http://www.arduino.cc/en/Tutorial/KeyboardButton + */ + +const int buttonPin = 4; // input pin for pushbutton +int previousButtonState = HIGH; // for checking the state of a pushButton +int counter = 0; // button push counter + +void setup() { + // make the pushButton pin an input: + pinMode(buttonPin, INPUT); +} + +void loop() { + // read the pushbutton: + int buttonState = digitalRead(buttonPin); + // if the button state has changed, + if ((buttonState != previousButtonState) + // and it's currently pressed: + && (buttonState == HIGH)) { + // increment the button counter + counter++; + // type out a message + Keyboard.print("You pressed the button: "); + Keyboard.print(counter); + Keyboard.println(" times."); + } + // save the current button state for comparison next time: + previousButtonState = buttonState; +} diff --git a/libraries/Keyboard/KeyboardSerial/KeyboardSerial.ino b/libraries/Keyboard/KeyboardSerial/KeyboardSerial.ino new file mode 100644 index 000000000..b64b6272b --- /dev/null +++ b/libraries/Keyboard/KeyboardSerial/KeyboardSerial.ino @@ -0,0 +1,33 @@ +/* + Keyboard test + + Reads a byte from the serial port, sends a keystroke back. + The sent keystroke is one higher than what's received, e.g. + if you send a, you get b, send A you get B, and so forth. + + The circuit: + * none + + created 21 Oct 2011 + by Tom Igoe + +This example code is in the public domain. + + http://www.arduino.cc/en/Tutorial/KeyboardSerial + */ + +void setup() { + // open the serial port: +Serial.begin(9600); +} + +void loop() { + // check for incoming serial data: + if (Serial.available() > 0) { + // read incoming serial data: + char inChar = Serial.read(); + // Type the next ASCII value from what you received: + Keyboard.write(inChar+1); + } +} +