Mouse Jiggle Tool – Create your own in C#

Mouse jiggling is rarely a useful tool or achievement except in cases where sending the computer to sleep would cause the interruption of another process – a system scan, render, or other such task which takes a large amount of time and you wish to leave it running over night. Even then, the usefulness of using a mouse jiggler is questionable when one can change the power settings of the computer, though they must remember to turn them back when they’ve finished of course.

However, as a training exercise, creating a mouse jiggle tool can be a good lesson in thinking about all eventualities of a program. Or it can just be a bit of fun. Either way, this post will hopefully give you all you need to create your own mouse jiggle tool in C#.

What is mouse jiggling?

Briefly, for those who might not know, mouse jiggling is the process of making the mouse appear to move of its own accord on a computer. Typically, it is made to jiggle around slightly for the purposes described above, however, sometimes it may make great leaps across the screen, if that is the programmers intention.

Creating a mouse jiggle tool

For this post, our mouse jiggle tool will contain only two operations – a start jiggle, and stop jiggle. Both are buttons which allows the user to, naturally, start or stop the jiggling.

Mouse Jiggle UI

Mouse Jiggle UI

In C#, you can assign functions to be run when buttons are clicked fairly easily by setting the click event handler to point to the name of your function. For this, we have assigned functions named StartJiggle_Click and StopJiggle_Click to the buttons in our UI. The code for these is as follows:

private void StartJiggle_Click(object sender, EventArgs e)
{
    doJiggle = true;
    t1 = new System.Threading.Thread(DoTheJiggle);
    t1.Start();
}

private void StopJiggle_Click(object sender, EventArgs e)
{
    doJiggle = false;
    MessageBox.Show("Jiggling stopped");
}

So what’s going on here? Well, ‘doJiggle’ is a global boolean variable which will be used to start and stop the jiggling process. Another way of doing this would be to stop the ‘t1’ thread variable, but as a belt and braces we’ve used the boolean. The ‘t1’ variable is a threading variable which ensures the UI doesn’t freeze when the mouse is jiggling. If the jiggling occurred on the same thread as the UI, the stop jiggle button would not work. The thread runs another function called ‘DoTheJiggle’ (I know, such imaginative function names eh…) which is set out below:

private void DoTheJiggle()
{
    while(doJiggle)
    {
        System.Drawing.Point pt = System.Windows.Forms.Cursor.Position;

        int moveUp = 0;

        Random rnd = new Random();
        int movement = rnd.Next(1, 2);
        switch (movement)
        {
            case 1:
                //Move up
                moveUp = -1;
               break;
            case 2:
                //Move down
                moveUp = 1;
                break;
        }

        pt.Y += moveUp;

        System.Windows.Forms.Cursor.Position = pt;
        System.Threading.Thread.Sleep(1000);
    }
}

Here we have a while loop which runs while our ‘DoJiggle’ variable is set to true. It grabs a random number between 1 and 2, then depending on that number it decides whether to move up or down with the mouse. You can extend this up to 4 numbers and include a left/right jiggle if you so wished. The ‘pt’ variable is the position of the mouse on the screen, you can obtain it by calling System.Windows.Forms.Cursor.Position and you can set it in the same way.

The thread goes to sleep for 1000 milliseconds to avoid the loop from trying to execute too quickly and obtaining the mouse point before it’s been reset. Try commenting out or removing this line and see the effects it has on your mouse as a result. A word of warning, make sure you’re happy with which thread is running the mouse jiggle tool in the task manager, you may find the only way to stop the jiggling is to end the process manually.

That’s all there is to it – you will now have a tool which jiggles your mouse up and down every second by one pixel, preventing your computer from sleeping (or perhaps being generally annoying). The bigger the number you give to the ‘moveUp’ variable, the bigger the jump for the mouse. If you were to be annoying to a friend, you could set this to be fairly high (and perhaps include a left/right setting as well) and remove the UI elements and set it running on a friends machine and watch them become increasingly frustrated by their jumping mouse.

Further work

Something you could include here is a keyboard listener to act on the press of the escape key (or a key of your choice) to stop or start the jiggling. You would probably need to use a global key hook (for C#) (pros and cons of which can be read here) so that you could listen for start and stop keys without the tool being in focus.

Leave a Reply

Your email address will not be published. Required fields are marked *