|
#define KEY_LEFT_CTRL 0x01 |
|
#define KEY_LEFT_SHIFT 0x02 |
|
#define KEY_RIGHT_CTRL 0x10 |
|
#define KEY_RIGHT_SHIFT 0x20 |
|
|
|
const int button1Pin = 2; |
|
const int button2Pin = 7; |
|
const int ledPin = 13; |
|
|
|
// Keycode 60 is F3. |
|
// See page 55 for more http://www.usb.org/developers/devclass_docs/Hut1_11.pdf |
|
const int f3KeyboardCode = 60; |
|
const int f7KeyboardCode = 64; |
|
const int vKeyboardCode = 25; |
|
const int pKeyboardCode = 19; |
|
|
|
int buttonState = 0; |
|
|
|
uint8_t buf[8] = { |
|
0 }; |
|
|
|
void setup() { |
|
Serial.begin(9600); |
|
// initialize the LED pin as an output: |
|
pinMode(ledPin, OUTPUT); |
|
// initialize the pushbutton pin as an input: |
|
pinMode(button1Pin, INPUT); |
|
pinMode(button2Pin, INPUT); |
|
} |
|
|
|
void loop() { |
|
|
|
if(readButton(button1Pin, f3KeyboardCode, pKeyboardCode)) |
|
{ |
|
} |
|
else if(readButton(button2Pin, f7KeyboardCode, vKeyboardCode)) |
|
{ |
|
} |
|
else |
|
{ |
|
digitalWrite(ledPin, LOW); |
|
keyOff(); |
|
} |
|
} |
|
|
|
|
|
boolean readButton(int pin, int keyboardCode, int switchModeCode) |
|
{ |
|
// read the state of the pushbutton value: |
|
buttonState = digitalRead(pin); |
|
|
|
// check if the pushbutton is pressed. |
|
// if it is, the buttonState is HIGH: |
|
if (buttonState == HIGH) { |
|
// turn LED on: |
|
digitalWrite(ledPin, HIGH); |
|
keyOn(keyboardCode, switchModeCode); |
|
|
|
return true; |
|
} |
|
|
|
return false; |
|
} |
|
|
|
void keyOn(int keyboardCode, int switchModeCode) |
|
{ |
|
buf[0] = KEY_LEFT_CTRL; |
|
buf[2] = switchModeCode; |
|
Serial.write(buf, 8); |
|
delay(10); |
|
|
|
keyOff(); |
|
|
|
buf[2] = keyboardCode; |
|
Serial.write(buf, 8); |
|
} |
|
|
|
void keyOff() |
|
{ |
|
buf[0] = 0; |
|
buf[2] = 0; |
|
Serial.write(buf, 8); |
|
} |
Wiring diagram is from the button tutorial http://arduino.cc/en/tutorial/button