Skip to content

Instantly share code, notes, and snippets.

@tmplinshi
Created April 9, 2016 09:27
Show Gist options
  • Save tmplinshi/889a14874ac943a2fe8245338feaca37 to your computer and use it in GitHub Desktop.
Save tmplinshi/889a14874ac943a2fe8245338feaca37 to your computer and use it in GitHub Desktop.
/*
; Holding down left Control key to lock, release to unlock.
~LControl::
LockCursorToPrimaryMonitor(true)
KeyWait, LControl
LockCursorToPrimaryMonitor(false)
return
*/
LockCursorToPrimaryMonitor(lock = true) {
; Get mouse position on screen
VarSetCapacity( pt, 8, 0 )
DllCall("GetCursorPos", "Ptr", &pt)
x := NumGet(pt, 0, "Int")
; If the cursor already on the second monitor, then leave it there.
if (x > A_ScreenWidth) {
return
}
; ClipCursor -- https://msdn.microsoft.com/en-us/library/windows/desktop/ms648383(v=vs.85).aspx
if (lock) {
VarSetCapacity(rect, 16, 0)
NumPut(A_ScreenWidth, rect, 8)
NumPut(A_ScreenHeight, rect, 12)
DllCall("ClipCursor", "ptr", &rect)
} else {
DllCall("ClipCursor", "ptr", 0)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment