DISCLAIMER: This tool is intended for educational and personal use only. There are legitimate uses for mouse jigglers — like preventing screen lock during presentations — but also potential for misuse. Please respect your workplace or organization’s IT policies. Misuse could violate acceptable use policies or even local laws.
A friend of mine recently asked me if I knew of a way to prevent their computer from going to sleep. They were working on a project that required them to be away from their computer for an extended period, and they didn't want to keep entering their password to unlock it. I suggested they use a mouse jiggler, a small device that moves the mouse cursor at regular intervals to prevent the computer from going to sleep. You can buy these devices online, but I thought it would be fun and fairly simple to build one myself.
There are a few ways of building a mouse jiggler, one way is to use a micro controller connected to a servo which interacts physically with the mouse to move it. An easier way is to use a micro controller that acts as a USB device and send mouse movement commands to the computer. I decided to go with the second option as it seemed simpler and less likely to break.
I looked in my drawer of random wires and micro controllers and found a Teensy 4.1 board. The Teensy is a small, powerful micro controller that can act as a USB device, which is fine for this prototype, probably a bit overkill maybe but it should do what I want it to do. I needed to install teensyduino to be able to program the Teensy, I followed the instructions on the Teensy website and installed the software, then was able to write some code to move the mouse cursor.
I thought I if could just get the mouse to move in a constant circle that should be enough. This is what I came up with, fairly simple and only took about 5 mins to write but I've commented it anyway just incase some people need to brush up on their geometry.
#include <math.h>
const int interval = 20; // Interval between updates (milliseconds)
const int radius = 5; // Radius of the circle
const int centerX = 0; // Center X position
const int centerY = 0; // Center Y position
const float speed = 0.05; // Speed of the movement (smaller is slower)
void setup() {
// Initialize the USB mouse functionality (Teensy does this automatically)
}
void loop() {
static unsigned long previousMillis = 0;
static float angle = 0; // Current angle for the circular motion
unsigned long currentMillis = millis();
// Check if it's time to update the mouse position
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
// Calculate new X and Y positions based on the angle
int x = centerX + radius * cos(angle);
int y = centerY + radius * sin(angle);
// Move the mouse to the new position
Mouse.move(x, y);
// Update the angle for the next iteration
angle += speed;
// Keep the angle within the range of 0 to 2*PI
if (angle >= 2 * PI) {
angle -= 2 * PI;
}
// Short delay to ensure the movement is smooth
delay(20);
}
}
Tada!
Using a small radius and slow speed, the mouse cursor moves in a small circle on the screen. I uploaded the code to the Teensy and connected it to my computer. The mouse cursor started moving in a circle, just as I had hoped. I left it running for a while to see if it would prevent the computer from going to sleep, and it worked! The computer stayed awake the whole time. I was happy with the prototype, but I wanted to make it a bit more user-friendly.
Currently it looks like this:
not great... but it works.
I've ordered a Adafruit Trinkey QT2040 which looks perfect for this project, it's small and plug-and-play. Theres no arduino library for it but I can figure that out when it arrives. Stay tuned!
I've been messing with this for a few days, theres a button on either side and I was wondering if it would be possible to use one of the buttons to turn the mouse jiggler on and off. This is what I came up with.
#include <math.h>
#include <Mouse.h>
#include <Adafruit_NeoPixel.h>
const int interval = 20; // Interval between updates (milliseconds)
const int radius = 5; // Radius of the circle
const int centerX = 0; // Center X position
const int centerY = 0; // Center Y position
const float speed = 0.05; // Speed of the movement (smaller is slower)
bool last_button = false;
bool pixel_on = false; // To track the state of the pixel
Adafruit_NeoPixel pixel(1, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800);
void setup() {
#if defined(NEOPIXEL_POWER)
pinMode(NEOPIXEL_POWER, OUTPUT);
digitalWrite(NEOPIXEL_POWER, HIGH);
#endif
pinMode(PIN_SWITCH, INPUT_PULLUP);
pixel.begin();
pixel.setBrightness(20); // not so bright!!!
}
uint8_t j = 0;
void loop() {
bool curr_button = !digitalRead(PIN_SWITCH);
if (curr_button && !last_button) {
Serial.println("Button pressed!");
pixel_on = !pixel_on; // Toggle the pixel state
if (pixel_on) {
pixel.setPixelColor(0, Wheel(j++));
pixel.show();
} else {
pixel.clear();
pixel.show();
}
}
if (!curr_button && last_button) {
Serial.println("Button released!");
}
last_button = curr_button;
static unsigned long previousMillis = 0;
static float angle = 0; // Current angle for the circular motion
unsigned long currentMillis = millis();
// Check if it's time to update the mouse position
if ((currentMillis - previousMillis >= interval)) {
previousMillis = currentMillis;
if (pixel_on) {
pixel.setPixelColor(0, Wheel(j++));
pixel.show();
// Calculate new X and Y positions based on the angle
int x = centerX + radius * cos(angle);
int y = centerY + radius * sin(angle);
// Move the mouse to the new position
Mouse.move(x, y);
// Update the angle for the next iteration
angle += speed;
// Keep the angle within the range of 0 to 2*PI
if (angle >= 2 * PI) {
angle -= 2 * PI;
}
}
}
}
uint32_t Wheel(byte WheelPos) {
if (WheelPos < 85) {
return pixel.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
} else if (WheelPos < 170) {
WheelPos -= 85;
return pixel.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else {
WheelPos -= 170;
return pixel.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
}
This is the same code as the prototype with a few extras, I've added a button to turn the mouse jiggler on and off and a LED to indicate the state of the mouse jiggler. The LED cycles through all the colours of the rainbow when the mouse jiggler is on and turns off when the mouse jiggler is off.
I MIGHT make a case for this, but I'm not sure yet. I'll see how it goes. For now it's finished I think.
© Karl Lankester-Carthy