Open a calculator window and run either of these scripts. The code is designed to press "5" on the standard size calc (normal mode, not scientific).
The simple version, with no "mouse retract":
Code:
~LButton & q::
WinActivate, Calculator
WinWaitActive, Calculator
MouseClick, left, 113, 169
return
The version with "mouse retract" (brings mouse cursor back to its starting position afterwards):
Code:
~LButton & q::
CoordMode, Mouse, Screen
MouseGetPos,mouseX,mouseY
CoordMode, Mouse, Relative
WinActivate, Calculator
WinWaitActive, Calculator
MouseClick, left, 113, 169
CoordMode, Mouse, Screen
MouseMove,mousex,mousey
return
Now, place the cursor over the desktop and hold the Lbutton. Obviously calculator will lose the focus. Press 'q' while holding the mouse button and you'll see the cursor flash over to the calculator window and then come back. You'll also see the title bar of the calc window flash, but the 5 button will not be pressed. If you use another window (like a notepad window) instead of just the desktop, you'll also notice that that window somehow regains the focus after the mouse zooms to the calc window and back.
If you keep Lbutton pressed after you release 'q', then press 'q' again, you'll achieve the desired effect - '5' will be pressed, and calc will maintain the focus afterwards. However, if you release Lbutton, then press it again to try the hotkey, you should be unsuccessful each time.
If you click somewhere
in the calc window during the hotkey - say, in one of the inactive areas - then press q, the script will work on the first try.
Notice also that if you make the following changes:
Code:
~LButton & q::
CoordMode, Mouse, Screen
MouseGetPos,mouseX,mouseY
CoordMode, Mouse, Relative
WinActivate, Calculator
WinWaitActive, Calculator
MouseClick, left, 113, 169
CoordMode, Mouse, Screen
MouseMove,mousex,mousey
wingettitle,title,A ;This title SHOULD be "Calculator"
msgbox, %title%
return
When you run the code now, clicking on the desktop to run the hotkey, the msgbox that opens will simply be empty, indicating that there is no active window (although, the window you clicked on would be active if you clicked a window instead of the desktop, even though the msgbox will still be blank). If you keep Lbutton pressed and press q a second time, this time the msgbox will show you that Calculator is active, which in fact it is. If you try to use a different hotkey, like #q, the script runs fine. Is there any way around this? I tried all of the following:
A version using absolute coords:
Code:
~LButton & q::
WinGetPos,winX,winY,,,Calculator
CoordMode, Mouse, Screen
clickx := winx + 113 ;figure out absolute coords
clicky := winy + 169
mouseclick,left,clickx,clicky
return
A complicated, roundabout version using MouseGetPos:
Code:
~LButton & q::
WinGetPos,winX,winY,,,Calculator
CoordMode, Mouse, Screen
clickx := winx + 113
clicky := winy + 169
MouseMove, clickx, clicky
MouseGetPos,,,MouseWin
WinActivate,ahk_id %MouseWin%
WinWaitActive,ahk_id %MouseWin%
Mouseclick, left ;the mouse is already over the button
return
All of these attempts should give you the same results, where the button is clicked on the second try. As far as I know, using #WinActivateForce doesn't change things. Is there any way to get this kind of hotkey to work, or is the mouseclick throwing things off? Obviously I can't just use "LButton & q::" instead of "~LButton & q::" because I'll lose all mouse functionality. (Incidentally, using "LButton & q::" does work, since no other window "steals" the focus when you press LButton.) Slowing down the mousespeed doesn't seem to help either. I'm at a loss
...Any ideas? Thanks for your input!
~Chris