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

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 Mouse

When you select "USB Keyboard/Mouse" from the Tools -> USB Type menu, the Teensy becomes a USB keyboard and mouse while running your program.

Mouse interaction is obviously limited because the Teensy can not view the screen. Nonetheless, you can send mouse actions, which might be useful for some types of projects.

Positioning The Mouse (Teensy LC & 3.0 & 3.1 Only)

Teensy3 supports positioning. First, use Mouse.screenSize(width, height) to configure Teensy for your screen. Then you can use Mouse.moveTo(x, y) to precisely position the mouse at any pixel on your screen.

#include <Bounce.h>

Bounce button4 = Bounce(4, 10);
Bounce button5 = Bounce(5, 10);

void setup() {
  Mouse.screenSize(1920, 1080);  // configure screen size
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);

void loop() {
  if (button4.fallingEdge()) {
    Mouse.moveTo(25, 100);       // point to pixel at 25, 100
  if (button5.fallingEdge()) {
    Mouse.moveTo(1580, 14);      // point to pixel at 1580, 14

For Macintosh computers, use Mouse.screenSize(width, height, true). This extra parameter tells Teensy to use an alternate algorithm for correct positioning on Apple's OS-X operating system.

X11 on Linux has a known limitation where Mouse.moveTo() does not work. This ugly hack can allow Mouse.moveTo() to work on Linux, by removing Mouse.move(). It also breaks Mouse support on Windows.

Moving The Mouse

To move the mouse, use Mouse.move(X, Y), where X and Y range from -127 to +127. Positive X moves to the right. Positive Y moves downwards. For natural looking motion, many small moves performed slowly are needed.

Here is a simple example that moves the mouse in a triangle.

void setup() { } // no setup needed
void loop() {
  int i;
  for (i=0; i<40; i++) {
    Mouse.move(2, -1);
  for (i=0; i<40; i++) {
    Mouse.move(2, 2);
  for (i=0; i<40; i++) {
    Mouse.move(-4, -1);

Video by Théo Reichel


For a simple mouse click, just use Use with caution!;

For more control over the 3 mouse buttons, you can use Mouse.set_buttons(LEFT, MIDDLE, RIGHT). For each input, 1 means the button is pressed, 0 means not pressed.

  Mouse.set_buttons(0, 0, 1);
  Mouse.set_buttons(0, 0, 0);

Click and Drag

You can combine Mouse.move() with Mouse.set_buttons() to perform drag and drop operations.

  Mouse.set_buttons(1, 0, 0);  // hold left button
  Mouse.move(-30, 10);         // move the mouse while holding
  Mouse.set_buttons(0, 0, 0);  // release button after move

Of course, how to position the mouse and where to move are tricky. You may also need to add substantial delay before the Mouse.set_buttons() call, because many GUI-based programs feature delayed respose, anticipating human-like timing.

Scroll Wheel

You can also control the scroll wheel. Positive numbers scroll upward; negative numbers scroll downward.