Skip to content

Instantly share code, notes, and snippets.

@bladeSk
Last active April 5, 2022 02:57
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bladeSk/9feeeb6c2ba9939faa3c88cc9133700c to your computer and use it in GitHub Desktop.
Save bladeSk/9feeeb6c2ba9939faa3c88cc9133700c to your computer and use it in GitHub Desktop.
AutoHotKey: Make mouse cursor follow window focus
; Makes the mouse cursor follow window focus, but ONLY if the focus change
; wasn't caused by the mouse - e.g. Alt-Tab, Win+<Number>, hotkeys, ...
; Saves a lot of mousing around on multi-monitor setups!
Gui +LastFound
lastMouseClickTime := 0
hWnd := WinExist()
DllCall("RegisterShellHookWindow", UInt, hWnd)
msgNum := DllCall("RegisterWindowMessage", Str, "SHELLHOOK")
OnMessage(msgNum, "OnShellMessage")
OnMessage(WM_MOUSEMOVE:=0x0201, "OnMouseDown")
Return
OnShellMessage( wParam, lParam )
{
global
; HSHELL_WINDOWACTIVATED | HSHELL_RUDEAPPACTIVATED
If (wParam = 4 or wParam = 32772) {
; ignore when dragging
GetKeyState, mouseDown, LButton
if (mouseDown <> "D" and A_TickCount - lastMouseClickTime > 500) {
; delay a tiny bit to ignore taskbar focus on Win+Number switching
Sleep, 10
CoordMode, Mouse, Screen
WinGetPos, wx, wy, width, height, A
; puts the cursor in the upper right corner of the active window, tweak to your needs
mx := Round(wx + width * 0.85)
my := Round(wy + height * 0.35)
DllCall("SetCursorPos", int, mx, int, my)
}
}
}
*~LButton::
lastMouseClickTime := A_TickCount
Return
*~RButton::
lastMouseClickTime := A_TickCount
Return
*~MButton::
lastMouseClickTime := A_TickCount
Return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment