Skip to content

Commit eeea208

Browse files
tigoeTom Igoe
authored andcommitted
Added examples for the Keyboard library of the Leonardo
0 parents  commit eeea208

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

KeyboardMessage/KeyboardMessage.ino

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
Keyboard Button test
3+
4+
Sends a text string when a button is pressed.
5+
6+
The circuit:
7+
* pushbutton attached from pin 4 to +5V
8+
* 10-kilohm resistor attached from pin 4 to ground
9+
10+
created 24 Oct 2011
11+
by Tom Igoe
12+
13+
This example code is in the public domain.
14+
15+
http://www.arduino.cc/en/Tutorial/KeyboardButton
16+
*/
17+
18+
const int buttonPin = 4; // input pin for pushbutton
19+
int previousButtonState = HIGH; // for checking the state of a pushButton
20+
int counter = 0; // button push counter
21+
22+
void setup() {
23+
// make the pushButton pin an input:
24+
pinMode(buttonPin, INPUT);
25+
}
26+
27+
void loop() {
28+
// read the pushbutton:
29+
int buttonState = digitalRead(buttonPin);
30+
// if the button state has changed,
31+
if ((buttonState != previousButtonState)
32+
// and it's currently pressed:
33+
&& (buttonState == HIGH)) {
34+
// increment the button counter
35+
counter++;
36+
// type out a message
37+
Keyboard.print("You pressed the button: ");
38+
Keyboard.print(counter);
39+
Keyboard.println(" times.");
40+
}
41+
// save the current button state for comparison next time:
42+
previousButtonState = buttonState;
43+
}

KeyboardSerial/KeyboardSerial.ino

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
Keyboard test
3+
4+
Reads a byte from the serial port, sends a keystroke back.
5+
The sent keystroke is one higher than what's received, e.g.
6+
if you send a, you get b, send A you get B, and so forth.
7+
8+
The circuit:
9+
* none
10+
11+
created 21 Oct 2011
12+
by Tom Igoe
13+
14+
This example code is in the public domain.
15+
16+
http://www.arduino.cc/en/Tutorial/KeyboardSerial
17+
*/
18+
19+
void setup() {
20+
// open the serial port:
21+
Serial.begin(9600);
22+
}
23+
24+
void loop() {
25+
// check for incoming serial data:
26+
if (Serial.available() > 0) {
27+
// read incoming serial data:
28+
char inChar = Serial.read();
29+
// Type the next ASCII value from what you received:
30+
Keyboard.write(inChar+1);
31+
}
32+
}
33+

0 commit comments

Comments
 (0)