skip navigational linksPJRC
Shopping Cart Checkout Shipping Cost Download Website
Home Products Teensy Blog Forum
You are here: Teensy Teensyduino USB Keyboard

PJRC Store
Teensy 3.6, $29.25
Teensy 3.5, $24.25
Teensy 3.2, $19.80
Teensy LC, $11.65
Teensy 2.0, $16.00
Teensy++ 2.0, $24.00
Main Page
Getting Started
How-To Tips
Code Library

Using USB Keyboard

Save your work!
Unexpected keystrokes can quickly ruin your program.
When you select "USB Keyboard" from the Tools -> USB Type menu, the Teensy becomes a USB keyboard and mouse while running your program.

Your PC or Mac will detect a new keyboard. Then your program can send keystrokes which your computer will recognize as coming from a standard USB keyboard.

There are two ways you can make your Teensy send USB keystrokes.

The Easy Way: Keyboard.print()

Keyboard.print() works the same way as Serial.print(), except the message is typed as keystrokes. You can print strings, numbers, single characters with all the same control as Serial.print().

Here is a very simple example, using Keyboard.print().

int count = 0;
void setup() { } // no setup needed
void loop() {
  Keyboard.print("Hello World "); 
  count = count + 1;

When this program is run, it will type "Hello World" and the increasing count. As you can see in this screenshot, the computer sees a USB keyboard and the keystrokes directly enter the Arduino editor. If delay(5000) had not been used, the result could be very unpleasant.

You may notice "Hello World 0" does not appear. The PC takes a brief time to detect the presence of a new USB device, but this program begins running immediately. If you use Keyboard.print() before the PC finishes the detection process (called "enumeration" in USB lingo), Keyboard.print() does nothing. A delay() can be added in setup(), if necessary.

Keyboard Layouts

The Tools > Keyboard Layout menu selects the type of keyboard Teensy will emulate. This setting takes effect when you Upload to Teensy.

If you see the wrong characters from Keyboard.print(), Teensy may be emulating a US keyboard layout, but your computer expects a non-US keyboard, or vise-versa. The layout setting only affects Keyboard.print() and Keyboard.write().

Using Pushbuttons

Many applications need to send messages to a computer when pushbuttons are pressed.

The Bounce library provides the easiest way to reliably detect pushbuttons changes. This code shows the basic approach. Just update() each object, and then use the fallingEdge() to check if it has changed.

void loop() {
  // Update all the button objects.

  // Check each button for "falling" edge.
  // falling = high (not pressed - voltage from pullup resistor)
  //           to low (pressed - button connects pin to ground)
  if (button1.fallingEdge()) {
    Keyboard.println("B1 press");
  if (button2.fallingEdge()) {
    Keyboard.println("B2 press");
  if (button3.fallingEdge()) {
    Keyboard.println("B3 press");

For a complete example: File > Examples > Teensy > USB_Keyboard > Buttons.

Individual Key Press / Release

You can use and Keyboard.release(key) to send individual key press and release events. This is similar to the "Micro Manager Way" described below, except the press() and release() functions will automatically take care of the details of setting 1 of the 6 possible key slots and sending the updates.

The Micro Manager Way

Sometimes you may need precise keyboard control. For example, many Windows programs will not recognize a CTRL-C character as a copy command unless the CTRL key is pressed and remains held down while the C key is pressed. Some keys, like "Home" and "Print Screen" can not be represented in a normal string usable with Keyboard.print(). The micro manager way requires more effort but gives you complete control.

The USB keyboard can have up to 6 normal keys and 4 modifier keys pressed at the same time. To micro manage the keyboard, you use functions to set which combination of keys you want pressed (and zero for the ones you want not pressed), and then use Keyboard.send_now() to send each combination.

You can use delay() or write code to check millis() between calling Keyboard.send_now(), to create the condition of a key held down similarly to a human typing. Auto-repeat is done by the driver on your PC or Mac, so if you have a long delay between pressing and releasing a normal key, the auto-repeat feature may deliver many key events to software.

When micro managing, you need to send a zero for each key you've pressed to cause that key to be released (as if a finger were lifting off the key and its spring were returning it to the resting position).

Modifier Keys

There are 4 modifier keys: Shift, Alt, Ctrl, and GUI. GUI is either the "windows key" (PC) or the "clover key" (Macintosh), usually located to the side of the space bar. These 4 keys are special, and can only be used with Keyboard.set_modifier().

MODIFIERKEY_GUIWindows (PC) or Clover (Mac)

If you want no modifier keys pressed, use a zero.


To press just one modifier is simple.


To press more than one modifier, use the logic OR operator. For example:


Normal Keys

Six keys are always tranmitted by the USB keyboard. Normally, only the first key is used and the other 5 are zero. However, you can use the 6 keys in any way you like. For example, this code would send keys A, B, C, D, E and F, all pressed at once.


To release a key, you must set it to zero. For the above example, all six would need to be set back to zero if the next Keyboard.send_now() should not have any of these 6 keys pressed.


After you have set the normal and modifier keys, use Keyboard.send_now() to communicate that combination to the PC or Mac.

The keys remain as you set them, so between each call to Keyboard.send_now(), you only need to make the changes you need. For example, this code would send a CTRL-ALT-DELETE sequence.

// press and hold CTRL

// press ALT while still holding CTRL

// press DELETE, while CLTR and ALT still held

// release all the keys at the same instant

It is important to set normal keys back to zero and send. If you do not, the key will remain pressed, and the PC or Mac USB driver will begin auto-repeat.

If you are familiar with the older PS/2 keyboard "scan codes", please be aware that USB keyboard codes work differently (and use different numerical values). The old scan codes sent different messages for key down and key up events, where each message represented a single key's change. With USB, each message represents the complete state of the keyboard. Up to any 6 keys may be pressed at once, plus any combination of the 8 modifier keys.

All Keys Codes

Normal Keys

TODO: document the 8 media key codes and the special set_media() function. Currently these only work on Mac and Linux. Windows does NOT work. A future version will redesign this feature to hopefully work on Windows too. For now, the only documentation is the example, in File > Examples > Teensy > USB_Keyboard > MediaButtons.

Bandwidth - Less Than You Might Expect

USB Keyboard speed is limited to 500 keystrokes per second, and some operating systems may limit the speed to only 62.5 keystrokes per second. There is nothing you can do about these limits. Reading the rest of this section will only help you understand why they exist.

The HID protocol is used by USB keyboards, and HID uses the USB "interrupt" transfer type, which allocates 1 packet per a configurable number of USB frames. Each USB frame is 1ms, and the Teensyduino USB keyboard code requests the interval to be every frame. If the operating system honors this request, bandwidth is available for 1000 packets per second.

Each keystroke requires 2 calls to Keyboard.send_now(), the first to press the key and the second to release it. This translates to 500 keystrokes per second. Of course, you can send 10 different keys in each, but logic inside the USB device driver will analyze for changes, which imposes a practical limit, especially when sending to already-written software which expects "normal" typing.

Some operating systems might disregard the request and instead schedule 1 packet every 8 frames. At 16ms each, only 62.5 keystrokes per second are possible.

Some software, even when running on a multi-gHz, multi-core processor may perform so much work per keystroke (or be written so infficiently), that sending at such "fast" speeds could overwhelm the computer. You might need to add extra delays.

USB keyboard is slow. If you need substantial bandwidth, use USB serial.