Teensy 3.1 – Emulating a USB Keyboard

My primary motivation for purchasing a Teensy is to emulate a keyboard to set up a little practical joke for someone.

So I need to get the Teensy running in a manner in which if I press a button, it will transmit text as if it were a keyboard attached to the PC.

This turns out to be quite easy. I have a push button switch attached to PIN 2 of my Teensy and I will be using the bounce library to read and debounce the switch.

Here is the code:

#include <Bounce.h>
int sw = 2; 
Bounce swBounced = Bounce(sw, 10); 

void setup() {                
  pinMode(sw, INPUT);
  digitalWrite(sw, HIGH);

void loop() {
  if (swBounced.risingEdge()) { // due to negative logic, button released on rising edge
    Keyboard.print("Hello World "); 

The action is all in the Keyboard.print statement. This is like the normal Arduino Serial.print statement except instead of transmitting the output to a USB serial port, it is going to transmit the output as if it where a keyboard.

Before compiling, you have to make sure the USB type is set to keyboard+mouse+joystick:


Now just compile and run as normal. Place the cursor into a window you don’t mind seeing ‘hello world’ in, and press the button:


The various methods for the Keyboard class can be found here:




This entry was posted in c-tinys and tagged , . Bookmark the permalink.

1 Response to Teensy 3.1 – Emulating a USB Keyboard

  1. Sean Straw says:

    if your vict^H^H^H^H target’s system supports bluetooth, your could extend this by pairing their system with your device, which would enable you to manipulate the input without an apparent device connected to their system, and without having to be right there to trigger it. Of course, they may have some big icon in their systray or similar that shows bluetooth activity, and to pair the devices, you’d have to have console access to the system (but that may be true for detecting a new HID device when you plug your teensy in via USB). If you untether, you need to independently power your device, but that could be done from a USB port of another system (or a wall wart). A bluetooth approach also remains fairly stealthy if say the target system is a laptop without an external keyboard, where the extra something dangling off of it would be readily noticed

    Another take would be to use an IR detector on another input and then you could use an IR remote (you can get a compact media type for less than US$1), which again would allow you to trigger without having to seem to be interacting with their workarea, and also, you have multiple codes you can enter or single-button trigger, allowing for multiple phrases/actions. This would be suitable either for a bluetooth or direct-cabled approach.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.