Last active
July 2, 2022 17:12
-
-
Save AlexRasch/6a09a3861b748d4e35c4dadcb69006f6 to your computer and use it in GitHub Desktop.
Basic PoC of mouse movement & distance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void Main(string[] args) | |
{ | |
/* | |
* Basic PoC of mouse movement & distance | |
* | |
* | |
*/ | |
Console.Title = "~~* Mouse Distance Poc *~~"; | |
Console.OutputEncoding = Encoding.UTF8; | |
Console.WriteLine("Press ALT + F4 to close"); | |
// Prevent closing by mistake | |
DeleteMenu(GetSystemMenu(GetConsoleWindow(), false), SC_CLOSE, MF_BYCOMMAND); | |
POINT currentMousePosition, previousMousePosition; | |
GetCursorPos(out previousMousePosition); | |
double mouseDistance = 0; | |
while (true) | |
{ | |
GetCursorPos(out currentMousePosition); | |
mouseDistance += Math.Sqrt( | |
Math.Pow(currentMousePosition.X - previousMousePosition.X, 2) + | |
Math.Pow(currentMousePosition.Y - previousMousePosition.Y, 2) | |
); | |
Thread.Sleep(100); | |
previousMousePosition = currentMousePosition; | |
Console.Write($"\rMouse distance: {ConvertDistance(mouseDistance).ToString("N")} cm"); | |
} | |
} | |
/// <summary> | |
/// Struct representing a point. | |
/// </summary> | |
[StructLayout(LayoutKind.Sequential)] | |
public struct POINT | |
{ | |
public int X; | |
public int Y; | |
public static implicit operator Point(POINT point) | |
{ | |
return new Point(point.X, point.Y); | |
} | |
} | |
/// <summary> | |
/// Retrieves the cursor's position, in screen coordinates. | |
/// </summary> | |
/// <see>See MSDN documentation for further information.</see> | |
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)] | |
[DllImport("user32.dll")] | |
public static extern bool GetCursorPos(out POINT lpPoint); | |
public static Point GetCursorPosition() | |
{ | |
POINT lpPoint; | |
GetCursorPos(out lpPoint); | |
// NOTE: If you need error handling | |
// bool success = GetCursorPos(out lpPoint); | |
// if (!success) | |
return lpPoint; | |
} | |
/// <summary> | |
/// Convert distance from pixel to cm | |
/// </summary> | |
/// <param name="mouseDistance"></param> | |
/// <returns> Distance in metrics</returns> | |
public static double ConvertDistance(double mouseDistance) => (mouseDistance * 0.0264583333); | |
// See https://docs.microsoft.com/en-us/windows/win32/menurc/wm-syscommand for details | |
private const int MF_BYCOMMAND = 0x00000000; | |
public const int SC_CLOSE = 0xF060; | |
public const int SC_MAXIMIZE = 0xF030; | |
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)] | |
[DllImport("user32.dll")] | |
public static extern int DeleteMenu(IntPtr hMenu, int nPosition, int wFlags); | |
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)] | |
[DllImport("user32.dll")] | |
private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert); | |
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)] | |
[DllImport("kernel32.dll", ExactSpelling = true)] | |
private static extern IntPtr GetConsoleWindow(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment