Processing Ajax...

Title

Message

Confirm

Confirm

Confirm

Confirm

Are you sure you want to delete this item?

Confirm

Are you sure you want to delete this item?

Lock mouse cursor to current monitor except while hotkey is held down

Description
This is an implementation of the requested feature on the forums of having the mouse cursor locked to the current monitor, except while a hotkey is held down.
This function combines the locking function with the unlocking function: Pressing and releasing this function's hotkey locks the mouse to the current monitor. It's then kept locked until you *press and hold down* the hotkey. While it's held down you can freely move between monitors. Once released, it gets locked again to the new current monitor.
Language
C#.net
Minimum Version
Created By
Venryx
Contributors
-
Date Created
Nov 9, 2018
Date Last Modified
Nov 9, 2018

Scripted Function (Macro) Code

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Threading;

public static class DisplayFusionFunction
{
    // update this value to match the hotkey you've assigned to this function -- ignoring ctrl, alt, etc. (I use Alt+A)
    static Keys hotkeyKey = Keys.A;

    [DllImport("user32")]
    static extern bool GetAsyncKeyState(int vKey);
    
	public static void Run(IntPtr windowHandle)
	{
        // if cursor is already unlocked by hotkey, don't start hotkey function again
        if (BFS.ScriptSettings.ReadValueBool("Cursor unlocked")) return;
	
		// unlock the mouse cursor since hotkey has been pressed down
		Cursor.Clip = Rectangle.Empty;
		BFS.ScriptSettings.WriteValueBool("Cursor unlocked", true);
		
		// wait until the hotkey is released
        while (GetAsyncKeyState((int)hotkeyKey)) {
			Thread.Sleep(10);
		}
		
		// then lock the mouse cursor again
		Cursor.Clip = BFS.Monitor.GetMonitorBoundsByMouseCursor();
		BFS.ScriptSettings.WriteValueBool("Cursor unlocked", false);
	}
}