Skip to content

Instantly share code, notes, and snippets.

@GER-NaN
Last active March 31, 2017 17:19
Show Gist options
  • Save GER-NaN/e2cda4f60d42f2ecdcbb7ce69702e17d to your computer and use it in GitHub Desktop.
Save GER-NaN/e2cda4f60d42f2ecdcbb7ce69702e17d to your computer and use it in GitHub Desktop.
A console app that moves the mouse and performs clicks.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
/*
Ideas from
http://stackoverflow.com/a/2416762/1363780
http://stackoverflow.com/a/24630693/1363780
*/
namespace MouseMover
{
class Program
{
[StructLayout(LayoutKind.Sequential)]
public struct MousePoint
{
public int X;
public int Y;
}
[DllImport("user32.dll", EntryPoint = "SetCursorPos")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetCursorPos(int X, int Y);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetCursorPos(out MousePoint lpMousePoint);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);
//Mouse actions
private const int MOUSEEVENTF_LEFTDOWN = 0x02;
private const int MOUSEEVENTF_LEFTUP = 0x04;
private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
private const int MOUSEEVENTF_RIGHTUP = 0x10;
static void Main(string[] args)
{
ConsoleKey key;
MousePoint point;
while ((key = Console.ReadKey(true).Key) != ConsoleKey.Escape)
{
GetCursorPos(out point);
if (key == ConsoleKey.LeftArrow)
point.X -= 10;
if (key == ConsoleKey.RightArrow)
point.X += 10;
if (key == ConsoleKey.UpArrow)
point.Y -= 10;
if (key == ConsoleKey.DownArrow)
point.Y += 10;
if (key == ConsoleKey.Spacebar)
{
//Call the imported function with the cursor's current position
uint X = (uint) point.X;
uint Y = (uint) point.Y;
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
}
SetCursorPos(point.X, point.Y);
}
}
}
}
@GER-NaN
Copy link
Author

GER-NaN commented Mar 31, 2017

The idea here is to eventually record actions of the mouse and then repeat them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment