Skip to content

Instantly share code, notes, and snippets.

@Kaizen86
Last active May 29, 2021 22:50
Show Gist options
  • Select an option

  • Save Kaizen86/04b9b74f07a450181453092e123096a1 to your computer and use it in GitHub Desktop.

Select an option

Save Kaizen86/04b9b74f07a450181453092e123096a1 to your computer and use it in GitHub Desktop.
Mouse Jiggler Source Code
using System;
using System.Threading;
using System.Drawing;
using System.Windows;
using System.Runtime.InteropServices;
namespace MouseJiggler
{
class Program
{
public static void Main(string[] args)
{
MouseMover mouse = new MouseMover();
double i = 0.0;
Random rng = new Random();
while (true)
{
//Teleport and click randomly around the screen
mouse.SendMouseData(
rng.Next(0, (int)SystemParameters.PrimaryScreenWidth),
rng.Next(0, (int)SystemParameters.PrimaryScreenHeight),
absolute:true,
(rng.Next(0,10)>=5?true:false,false,false));
/*int x = (int)((Math.Sin(i)) * 10);
int y = (int)((Math.Cos(i)) * 10);
mouse.SendMouseData(x, y);
i += 0.1;*/
Thread.Sleep(1);
}
}
class MouseMover
{
//Mouse movement code
//Adapted from:
//https://social.msdn.microsoft.com/Forums/en-US/83650dd5-baf6-4028-a4af-6c91ef464412/is-there-a-way-to-programatically-hold-down-the-mouse-buttons?forum=csharpgeneral
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
private static extern void mouse_event(uint dwFlags, int dx, int dy, int cButtons, int dwExtraInfo); //Original used long instead of uint, which is wrong https://stackoverflow.com/questions/9855438/a-call-to-a-pinvoke-function-has-unbalanced-the-stack
private enum MOUSEEVENTF : uint //Enum defining mouse event flags
{
MOVE = 0x01,
ABSOLUTE = 0x8000,
LEFTDOWN = 0x02,
LEFTUP = 0x04,
RIGHTDOWN = 0x08,
RIGHTUP = 0x10,
MIDDLEDOWN = 0x20,
MIDDLEUP = 0x40
}
bool lmb_prev, mmb_prev, rmb_prev = false;
public void SendMouseData(int X, int Y, bool absolute = false, (bool lmb, bool mmb, bool rmb)? click_flags = null)
{
//Deconstruct the click_flags tuple into three separate booleans
click_flags = click_flags ?? (false, false, false);
(bool lmb, bool mmb, bool rmb) = click_flags.Value;
MOUSEEVENTF FLAGS = MOUSEEVENTF.MOVE; //Set the movement flag, otherwise the mouse will not move at all.
//Set the ABSOLUTE flag if the input boolean is set.
FLAGS |= absolute ? MOUSEEVENTF.ABSOLUTE : 0;
//If absolute mode is set, we have to scale X and Y to 0-65535 instead of supplying the normal pixel coordinates.
if (absolute)
{
X = Map(X, 0, (int)SystemParameters.PrimaryScreenWidth, 0, 65535);
Y = Map(Y, 0, (int)SystemParameters.PrimaryScreenHeight, 0, 65535);
}
//Perform OR operations to set all the mouseclick flags, but only if the associated variable has changed since last time.
//This check is necessary because repeatedly setting the flags will trigger events in programs listening for those flags.
if (lmb != lmb_prev)
{
FLAGS |= lmb ? MOUSEEVENTF.LEFTDOWN : MOUSEEVENTF.LEFTUP;
lmb_prev = lmb;
}
if (mmb != mmb_prev)
{
FLAGS |= mmb ? MOUSEEVENTF.MIDDLEDOWN : MOUSEEVENTF.MIDDLEUP;
mmb_prev = mmb;
}
if (rmb != rmb_prev)
{
FLAGS |= rmb ? MOUSEEVENTF.RIGHTDOWN : MOUSEEVENTF.RIGHTUP;
rmb_prev = rmb;
}
//Finally, send the API call.
mouse_event((uint)FLAGS, X, Y, 0, 0);
}
//Port of the Arduino's map function because it's so darn useful
private int Map(int value, int fromSource, int toSource, int fromTarget, int toTarget)
{
//Cast all arguments to double since the formula needs to use doubles
double dvalue = (double)value;
double dfromSource = (double)fromSource;
double dtoSource = (double)toSource;
double dfromTarget = (double)fromTarget;
double dtoTarget = (double)toTarget;
return (int)((dvalue - dfromSource) / (dtoSource - dfromSource) * (dtoTarget - dfromTarget) + dfromTarget);
}
//Adapted from https://stackoverflow.com/questions/1316681/getting-mouse-position-in-c-sharp
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
private static extern bool GetCursorPos(out Point lpPoint);
public Point GetCurrentPosition()
{
Point lpPoint;
GetCursorPos(out lpPoint);
return lpPoint;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment