Last active
August 10, 2024 00:53
-
-
Save jdhenckel/ecb07bb713a4a6b7c0b4ee01e3fda4b8 to your computer and use it in GitHub Desktop.
You can run this in the background and it will move your mouse to a nice location if you leave it idle for long enough time.
This file contains hidden or 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
using System.Runtime.InteropServices; | |
using System; | |
using System.Numerics; | |
using System.Threading; | |
namespace MouseMinder | |
{ | |
/*************** | |
* John Henckel, [email protected] 1 OCT 2020 | |
* | |
* You can run this in the background and it will move your mouse to a nice location | |
* if you leave it idle for long enough time. | |
* | |
* Compiled with VS 2017, dotNet 4.7, CONSOLE app, tested on Windows 10 | |
* | |
* Release: CC-BY https://creativecommons.org/licenses/by/4.0/ | |
*/ | |
class Program | |
{ | |
/*************** | |
* WIN32 declarations | |
*/ | |
[StructLayout(LayoutKind.Sequential)] | |
public struct POINT | |
{ | |
public int X; | |
public int Y; | |
public static implicit operator Vector2(POINT point) | |
{ | |
return new Vector2(point.X, point.Y); | |
} | |
} | |
[DllImport("user32.dll")] | |
public static extern bool GetCursorPos(out POINT lpPoint); | |
[DllImport("user32.dll")] | |
public static extern bool SetCursorPos(int x, int y); | |
/********************* | |
* Main entry point | |
*/ | |
static void Main(string[] args) | |
{ | |
new Program(args).MainLoop(); | |
} | |
Program(string[] args) | |
{ | |
Console.WriteLine("If you stop moving the mouse for a while, this program will return the mouse to the home position."); | |
Console.WriteLine("Before you run this program, put the mouse where you want the home."); | |
if (args.Length > 0) idleLimit = int.Parse(args[0]); | |
if (args.Length > 1) speed = int.Parse(args[1]); | |
Console.WriteLine("\nParmeters: idle seconds = " + idleLimit + ", speed = " + speed + "\n"); | |
} | |
private readonly int idleLimit = 20; | |
private readonly float speed = 20; | |
/********************* | |
* Main loop | |
*/ | |
void MainLoop() | |
{ | |
var p = GetCursorPosition(); | |
Console.WriteLine("Home Position is " + p); | |
var n = 0; | |
var q = GetCursorPosition(); | |
for (;;) | |
{ | |
// n is the number of seconds the mouse has been idle | |
Thread.Sleep(5000); | |
n += 5; | |
if (Vector2.DistanceSquared(q, GetCursorPosition()) > 2) | |
{ | |
n = 0; | |
q = GetCursorPosition(); | |
} | |
else if (n > idleLimit) GlideMouse(p); | |
} | |
} | |
// Try to move the mouse to position. If user touches the mouse, stop trying | |
void GlideMouse(Vector2 t) | |
{ | |
var p = GetCursorPosition(); | |
if ((t - p).Length() < 2) return; | |
Console.WriteLine(" returning to "+t); | |
for (;;) | |
{ | |
Thread.Sleep(10); | |
if (Vector2.DistanceSquared(p, GetCursorPosition()) > 2) break; // user wiggled the mouse! | |
var d = t - p; | |
var len = d.Length(); | |
if (len < 2) return; // arrived at target location | |
p += d * speed / Math.Max(len, speed); | |
SetCursorPosition(p); | |
} | |
Console.WriteLine("User interrupted the glide"); | |
} | |
void SetCursorPosition(Vector2 v) | |
{ | |
if (!SetCursorPos((int) v.X, (int) v.Y)) | |
Console.WriteLine("SetCursorPos returned false"); | |
} | |
Vector2 GetCursorPosition() | |
{ | |
if (GetCursorPos(out POINT lpPoint)) return lpPoint; | |
Console.WriteLine("GetCursorPos returned false"); | |
return Vector2.Zero; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello @jdhenckel Can you build this code and publish the executable version?