Weekly Tutorial: Bad USB with Arduino

By Savalio (Edited by Swee for updates)

Yes, this is real! You don’t have to spend $100 on a practically illegal item for a funny prank or to educate your loved ones! In this guide, I will explain how to use USB HID libraries.

Part 0: Legality yap

Please be VERY sure that whatever your doing with this project is legal and follows the policies of the place if you’re doing it at a public place. DO NOT use this on stranger’s computers. I wish you happy jail-free fun!

Part 1: Required items

Here’s your shopping list for the project:

  • Arduino capable of HID. This includes but is not limited to Arduino Leonardo, Arduino Nano ESP32, Arduino Due, Arduino Uno R4(both versions), etc.

  • A cable to program the board.

Optional:

  • A wire to short B1 and GND while programming.

  • A breadboard for ease of use and shorting B1 and GND.

  • A dummy computer to make sure you don’t ruin the main one.

Part 2: Code (Mouse)

Now open Arduino IDE, and paste in this code provided in the official Arduino Nano ESP32 cheatsheet:

#include "USB.h"
#include "USBHIDMouse.h"
USBHIDMouse Mouse;

void setup() {
  // put your setup code here, to run once:
  Mouse.begin();
  USB.begin();
}

void loop() {
  // put your main code here, to run repeatedly:
  Mouse.move(5, 0, 0); 
  delay(50);
}

If you upload this to your Arduino and keep it plugged in, it should start moving your mouse! We can easily modify the script to turn it into an annoying mouse jitterer:

#include "USB.h"
#include "USBHIDMouse.h"
USBHIDMouse Mouse;

void setup() {
  // put your setup code here, to run once:
  Mouse.begin();
  USB.begin();
}

void loop() {
  // put your main code here, to run repeatedly:
  Mouse.move(random(-50, 50), random(-50, 50), 0); 
  delay(50);
}

Now upload the code again and see your mouse jitter like crazy!

Tip: I recommend turning on boot loader mode on your Arduino before uploading, because uploading will get difficult to do!

Here’s the syntax for the function Mouse.move():

mouseObj.move(int x, int y, int wheel, int pan);

Now time to talk clicks. Paste this code into Arduino IDE:

#include "USB.h"
#include "USBHIDMouse.h"
USBHIDMouse Mouse;

void setup() {
  // put your setup code here, to run once:
  Mouse.begin();
  USB.begin();
}

void loop() {
  // put your main code here, to run repeatedly:
  Mouse.click(); 
  delay(50);
}

Now unplug the board and repeatedly left click somewhere on the screen. Now plug in the board, and you should see the exact same behavior.

Here’s Mouse.click() syntax:

mouseObj.click(uint8_t button = MOUSE_LEFT);

NOTE: You don’t have to stick to the left click. You can do middle and right clicks too!

And last mouse functions: Mouse.press() and Mouse.release(), where Mouse.press() acts like a click and hold, and Mouse.release() acts like a button release. Syntax is similar to Mouse.click().

Part 3: Code (Keyboard)

Mouse was pretty fun, but keyboard is even more fun! You can write words, you can activate shortcuts, and more with pressing keys! But before that, lets first write something with it, just to see. Here’s Arduino-provided example of the keyboard:

#include "USB.h"
#include "USBHIDKeyboard.h"
USBHIDKeyboard Keyboard;

void setup() {
  // put your setup code here, to run once:
  Keyboard.begin();
  USB.begin();
}

void loop() {
  // put your main code here, to run repeatedly:
  Keyboard.print("Hello World");
  delay(500);
}

Now hit upload and click on a search bar or inside the code editor. You will see Hello World! being typed! You can basically use a text file as a serial monitor! Here’s the syntax for the Keyboard.print() function:

keyboardObj.print(const char * var);

Now, let’s get to keys! Paste this into the Arduino IDE:

#include "USB.h"
#include "USBHIDKeyboard.h"
USBHIDKeyboard Keyboard;

// Windows and Linux:
char ctrlKey = KEY_LEFT_CTRL;
// MacOS X:
//char ctrlKey = KEY_LEFT_GUI;

void setup() {
  Keyboard.begin();
  USB.begin();
}

void loop() {
  delay(1000);
  Keyboard.press(ctrlKey);
  Keyboard.press('n');
  delay(100);
  Keyboard.releaseAll();
}

After you hit upload, don't exit Arduino IDE. And in a second after the sketch uploads, you should see a new Arduino IDE window pop up. And this was done with the help of the guys named Keyboard.press()(presses the keys) and Keyboard.releaseAll()(releases the keys). Here's the syntax for both:

keyboardObj.press(uint8_t key);
keyboardObj.releaseAll();

Now, let’s make some more complicated projects with this. I won’t go too advanced (i.e. using other libraries, more than 100 lines of code), but you definitely can!

Part 4: Example projects

1: Chaotic mouse

This will not only jitter the mouse like crazy, it will also make it randomly click and hold buttons!

WARNING: Please save all important files before uploading the code!

Here’s the code:

#include "USB.h"
#include "USBHIDMouse.h"
USBHIDMouse Mouse;

void setup() {
  Mouse.begin();
  USB.begin();
}

void loop() {
  int pressChoice = random(0, 3);
  int pressButChoice = random(0, 3);
  if (pressChoice == 2) {
    if (pressButChoice == 0) {
      Mouse.press();
    } else if (pressButChoice == 1) {
      Mouse.press(MOUSE_MIDDLE);
    } else {
      Mouse.press(MOUSE_RIGHT);
    }
  } else if (pressChoice == 1) {
    if (pressButChoice == 0) {
      Mouse.click();
    } else if (pressButChoice == 1) {
      Mouse.click(MOUSE_MIDDLE);
    } else {
      Mouse.click(MOUSE_RIGHT);
    }
  } else {
    Mouse.release();
    Mouse.release(MOUSE_MIDDLE);
    Mouse.release(MOUSE_RIGHT);
  }
  Mouse.move(random(-100, 100), random(-100, 100)); 
  delay(50);
}

Now, be ready for the chaos! I tested this on my school laptop and accidentally resized all the shortcuts…

2: Tab closing thing

Your friend took embarrassed you for no reason? Got you in trouble at school or with your parents? Now you can get back! Close all their windows, which will demolish their progress in a game, destroy their unsaved part of the essay, remove part of their class notes and more.

Code to activate on Windows/Linux(shortcut tested on Ubuntu):

#include "USB.h"
#include "USBHIDKeyboard.h"
USBHIDKeyboard Keyboard;

char altKey = KEY_LEFT_ALT;
char f4Key = KEY_F4;

void setup() {
  Keyboard.begin();
  USB.begin();
}

void loop() {
  delay(250);
  Keyboard.press(altKey);
  Keyboard.press(f4Key);
  delay(100);
  Keyboard.releaseAll();
}

And this code for MacOS X:

#include "USB.h"
#include "USBHIDKeyboard.h"
USBHIDKeyboard Keyboard;

char cmdKey = KEY_LEFT_GUI;
char f4Key = KEY_F4;

void setup() {
  Keyboard.begin();
  USB.begin();
}

void loop() {
  delay(250);
  Keyboard.press(cmdKey);
  Keyboard.press(f4Key);
  delay(100);
  Keyboard.releaseAll();
}

Now plug it in when they’re not looking and see your burning revenge!

See more in the Part 2!

Sorry, there’s no part 2 coming anytime soon!

Comments

© 2025 Swee, Savalio, and other SweeBlogs contributors.
Made with Jekyll