PDA

View Full Version : Teensy 2.0 USB Keyboard - Integer Value or Serial.print?



WedgeVLP
01-28-2013, 08:35 PM
I'm totally new to this, but I managed to build a simple box with 4 buttons to send keyboard commands via USB. I've got everything working, including some cool LEDs, and I've included the Bounce library to help weed out the button chatter, but I'm scratching my head on the Keyboard.write command.

I'm able to print a Spacebar press by using Keyboard.write(32). That works great. Unfortunately, no matter what I've tried (integer value, hex or octal format), I can't seem to get the other buttons to fire off an "Esc" or Up or Down Arrows.

I tried the following different iterations of this line for the ESC key, but no dice:

Keyboard.write(0x1B)

Keyboard.write(27)

Keyboard.write(\033)

Keyboard.write(KEY_ESC)

I also tried all of the above with Keyboard.print and Serial.print, to no avail. I've had the same experience with the up and down arrows (KEY_UP_ARROW & KEY_DOWN_ARROW). I'm sure I'm just missing something basic here...any advice would be appreciated.

I've scoured the web for more info on modifier keys and ASCII tables and how to implement them, but nothing worked. I'm programming with Arduino 1.0.3 on Win7 x64, and Teensyduino seems to be integrated correctly. I've selected USB > Serial+Keyboard+Mouse+Joystick, and I can get it to print/write any other basic keys (or strings/messages, i.e. "Hello world!")...just not the ones I need.

Thanks in advance for the help...and for creating such a useful and fun product!

PaulStoffregen
01-28-2013, 09:09 PM
Try Keyboard.press(KEY_ESC) and Keyboard.release(KEY_ESC)

Nantonos
01-28-2013, 09:45 PM
So there I was, about to confidently recommend

#include "keylayouts.h"

void setup (){
usb_keyboard_press(KEY_ESC, 0);
}

void loop (){
}
which compiles fine on Teensy 3.0. But if I switch to Teensy 2.0, I get



kbd_test01b.ino: In function 'void setup()':
kbd_test01b:4: error: 'usb_keyboard_press' was not declared in this scope

Nantonos
01-28-2013, 09:52 PM
Looks as if that function was added for Teensy 3.0, but not for Teensy 2.0. So (until that is fixed) add

void usb_keyboard_press(int key, int modifier) {
// press one key with up to one modifier, then release
Keyboard.set_modifier(modifier);
Keyboard.set_key1(key);
Keyboard.set_key2(0);
Keyboard.set_key3(0);
Keyboard.set_key4(0);
Keyboard.set_key5(0);
Keyboard.set_key6(0);
Keyboard.send_now();
delay(1);
Keyboard.set_key1(0);
Keyboard.send_now();
}

WedgeVLP
01-28-2013, 10:47 PM
Thanks, Paul...press/release worked like a charm on the first try for all three (KEY_ESC, KEY_UP_ARROW and KEY_DOWN_ARROW). It figures! :D

Nantonos...I'll save your idea for another elegant solution...thanks for your input.