Skip to main content

Creating a macOS Mouse Jiggler without Using Third-Party Libraries

· 3 min read
Joie Llantero

Keep your Mac awake with a simple mouse jiggler written in C. While I usually prefer Python for scripting, I wanted to avoid relying on external packages for this project, eliminating the need for setting up a virtual environment. Instead, I chose C to leverage macOS's native Application Services framework.

The Application Services framework provides essential APIs for tasks like graphics rendering, image handling, accessibility, and more. Using the Core Graphics component, I can build a program to automatically move the mouse pointer to random locations on the screen.

The first step is to create the main() function, where we’ll initialize the random number generator. By default, the generator produces the same sequence each time the program runs. To make it truly random, we’ll seed it with a unique value using the time() function, as the system time changes with every program execution.

int main() {
// initialize starting point of random number generator
srand((unsigned int)time(NULL));

while (1) {
int randomNumber = rand();
printf("Random Number: %d\n", randomNumber);
// generate a random number every 3 seconds
sleep(3);
}
return 0;
}

The output of the program above should be something like this:

$ ./test
Random Number: 1307726537
Random Number: 1612263961
Random Number: 371734681

The next step is to create the jiggleMouse() function. Imagine that the Quandrants of the Coordinate Plane is on top of your screen. The function's purpose is to obtain the current x and y coordinates of the mouse pointer and add the random numbers generated earlier. With this, the new mouse pointer location will move along the x and y axis of the screen. It's also important to note that we need to release the memory used by our program using CFRelease() once we are done with the process. See the completed function below.

void jiggleMouse() {
// get the current mouse position
CGEventRef event = CGEventCreate(NULL);
CGPoint currentPos = CGEventGetLocation(event);
CFRelease(event);

// generate random movement values
int deltaX = (rand() % 21) - 10; // Random value between -10 and 10
int deltaY = (rand() % 21) - 10; // Random value between -10 and 10

// move the mouse to a new position
CGPoint newPos = CGPointMake(currentPos.x + deltaX, currentPos.y + deltaY);
CGEventRef move = CGEventCreateMouseEvent(NULL, kCGEventMouseMoved, newPos, kCGMouseButtonLeft);
CGEventPost(kCGHIDEventTap, move);
CFRelease(move);

printf("Mouse moved to: (%.0f, %.0f)\n", newPos.x, newPos.y);
}

Before running the program, change sleep(3); to sleep(60); so that the mouse will not move too often. This will move the mouse every 60 seconds. You can also change this to your own preference. Afterwards, run the program and it should show similar outputs below.

$ ./mouse_jiggler
Mouse moved to: (959, 569)
Mouse moved to: (956, 571)

Note that the output coordinates shown in my example above will be different with your's.

Completed Program

Clone the repository here.. Give it a star if you liked it or let me know any issues.