Could anyone show me the Teensy 2.0 Keyboard Code?

Status
Not open for further replies.

brbeaton

New member
This is my first project and I don't have much coding experience beyond the Arduino Uno projects. I have created a single pushbutton circuit with my Teensy 2.0 in hopes of completing the keyboard project on the Teensy 2.0 tutorial site (https://www.pjrc.com/teensy/usb_keyboard.html). I want to code the Teensy so that the push button creates a keystroke on screen (e.g., I push the button and send the letter "B" to my computer). There doesn't seem to be any simple code for the keyboard on the website. Could anyone show me how to program a single key so that I can finish the project?
 
How to program a keyboard on the Teensy 2.0

Thanks, I managed to figure out what was going on using the file->examples->teensy->USB_KEYBOARD>Buttons example.

For those of you who are new to programming, you should know a few things about creating a keyboard. Firstly, while it appears that the perfect example was imbedded in the files for the chip, the online tutorials are not clear that resistors are only needed for certain chips. The Teensy 2.0 doesn't need them in this case (though if wonder if it ever does). Secondly, the code "Button0" and "Button1" means the button wired to pin 0 and pin 1 respectively. It may seem odd that I had to figure that out (and attempting to use resistors proved a timely obstacle), but I'm brand new to this. The developers could easily provide laymen's tutorials that are much clearer. I'd write them for free (pics included).

I was able to alter the example so that my pushbutton typed an "A" when I pressed it. By simply deleting the code for the "release" function I was able to make sure that it only typed an "A". I was even able to alter the code so that a different type of switch types when it is pressed (one that completes the circuit when pressed instead of interrupting it). See my code below:

/* Buttons to USB Keyboard Example

You must select Keyboard from the "Tools > USB Type" menu

This example code is in the public domain.
*/

#include <Bounce.h>

// Create Bounce objects for each button. The Bounce object
// automatically deals with contact chatter or "bounce", and
// it makes detecting changes very simple.
Bounce button0 = Bounce(0, 10);
Bounce button1 = Bounce(1, 10); // 10 = 10 ms debounce time
Bounce button2 = Bounce(2, 10); // which is appropriate for
Bounce button3 = Bounce(3, 10); // most mechanical pushbuttons
Bounce button4 = Bounce(4, 10);
Bounce button5 = Bounce(5, 10); // if a button is too "sensitive"
Bounce button6 = Bounce(6, 10); // to rapid touch, you can
Bounce button7 = Bounce(7, 10); // increase this time.
Bounce button8 = Bounce(8, 10);
Bounce button9 = Bounce(9, 10);

void setup() {
// Configure the pins for input mode with pullup resistors.
// The pushbuttons connect from each pin to ground. When
// the button is pressed, the pin reads LOW because the button
// shorts it to ground. When released, the pin reads HIGH
// because the pullup resistor connects to +5 volts inside
// the chip. LOW for "on", and HIGH for "off" may seem
// backwards, but using the on-chip pullup resistors is very
// convenient. The scheme is called "active low", and it's
// very commonly used in electronics... so much that the chip
// has built-in pullup resistors!
pinMode(0, INPUT_PULLUP);
pinMode(1, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP); // Teensy++ LED, may need 1k resistor pullup
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
}

void loop() {
// Update all the buttons. There should not be any long
// delays in loop(), so this runs repetitively at a rate
// faster than the buttons could be pressed and released.
button0.update();
button1.update();
button2.update();
button3.update();
button4.update();
button5.update();
button6.update();
button7.update();
button8.update();
button9.update();

// Check each button for "falling" edge.
// Type a message on the Keyboard when each button presses
// Update the Joystick buttons only upon changes.
// falling = high (not pressed - voltage from pullup resistor)
// to low (pressed - button connects pin to ground)B5 release

if (button1.fallingEdge()) {
Keyboard.println("B1 press");
}
if (button2.fallingEdge()) {
Keyboard.println("B2 press");
}
if (button3.fallingEdge()) {
Keyboard.println("B3 press");
}
if (button4.fallingEdge()) {
Keyboard.println("B4 press");
}
if (button5.fallingEdge()) {
Keyboard.println("B5 press");
}
if (button6.fallingEdge()) {
Keyboard.println("B6 press");
}
if (button7.fallingEdge()) {
Keyboard.println("B7 press");
}
if (button8.fallingEdge()) {
Keyboard.println("B8 press");
}
if (button9.fallingEdge()) {
Keyboard.println("B9 press");
}

// Check each button for "rising" edge
// Type a message on the Keyboard when each button releases.
// For many types of projects, you only care when the button
// is pressed and the release isn't needed.B0 pressB5 release

// rising = low (pressed - button connects pin to ground)
// to high (not pressed - voltage from pullup resistor)
if (button0.risingEdge()) {
Keyboard.println("A");
}
if (button1.risingEdge()) {
Keyboard.println("B1 release");
}
if (button2.risingEdge()) {
Keyboard.println("B2 release");
}
if (button3.risingEdge()) {
Keyboard.println("B3 release");
}
if (button4.risingEdge()) {
Keyboard.println("B4 release");
}
if (button5.risingEdge()) {
Keyboard.println("B5 release");
}
if (button6.risingEdge()) {
Keyboard.println("B6 release");
}
if (button7.risingEdge()) {
Keyboard.println("B7 release");
}
if (button8.risingEdge()) {
Keyboard.println("B8 release");
}
if (button9.risingEdge()) {
Keyboard.println("B9 release");
}
}
 
Status
Not open for further replies.
Back
Top