Tuesday, February 1, 2011

Center mouse after aero snap keyboard shortcut (auto hotkey)

True X-Mouse has a few problems. The most noticeable (for me) is that if the window the mouse is over moves (ie: you use the Aero Snap keyboard shortcuts, practically my favorite command in Windows 7), the window focus changes to the window below it. (This can really mess with your muscle memory!)

Turns out AutoHotKey can remedy the situation (I'm new to AutoHotKey, so there's probably a more elegant way to do this):

; Aero-Snap Mouse
; Move mouse to center of windows that are aero-snapped (True X-Mouse won't do this)
CenterMouseInWindow(Title)
{
WinActivate, %Title% ; True X-Mouse may have taken focus away when we moved
WinGetPos, X, Y, width, height, %Title%
center_x:=width/2
center_y:=height/2
MouseMove,center_x,center_y
return
}

#Left::
WinGetTitle, Title, A
SendInput #{Left}
CenterMouseInWindow(Title)
return

#+Left::
WinGetTitle, Title, A
SendInput #+{Left}
CenterMouseInWindow(Title)
return

#Right::
WinGetTitle, Title, A
SendInput #{Right}
CenterMouseInWindow(Title)
return

#+Right::
WinGetTitle, Title, A
SendInput #+{Right}
CenterMouseInWindow(Title)
return

#Up::
WinGetTitle, Title, A
SendInput #{Up}
CenterMouseInWindow(Title)
return

#Down::
WinGetTitle, Title, A
SendInput #{Down}
CenterMouseInWindow(Title)
return



This captures the aero snap shortcuts, gets the title for the current window, sends the aero snap shortcut to windows (which moves the window), then re-activates the old window and centers the mouse.

It's not instant (even with MoveMouse speed=0, I tried it), so you can't do Win+Shift+Left/Right rapid fire, but it works pretty well other than that.

No comments: