Skip to main content

Creating a macOS Mouse Jiggler without Using Third-Party Libraries

· 3 min read
Joie Llantero

Prevent your Mac from sleeping using a simple mouse jiggler coded in C programming language.

I like using Python to create scripts but I wanted to avoid using any packages for a mouse jiggler so that there's no need to setup a virtual environment. Thus, I opted to use C programming language to use the macOS framework, Application Services.

Application Services provide essential APIs for graphics rendering, image handling, accessibility, and more. With the Core Graphics component, I can create a program that will automatically move the mouse pointer at a random location on my screen.

First step is to create the main() function where we will first seed or initialize the random number generator. The generator will produce the same sequence each time we run the program by default. To make it random, we will initialize it with a different value using the time() function. The reason for this is time changes for each run of the program.

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.