I tried to save the caret position whenever left-clicked, but it seemed impossible, because reading the built-in variable A_CaretX/Y prevents double-clicking:
Code:
LButton::
cX := A_CaretX
Send {LButton Down}
Return
~LButton Up:: Return
It's caused by Dll call "AttachThreadInput" and AHK-documented: "... There is no known workaround.".
Now there is for XP at least, not Windows 2000 (as MSDN states)!
I gave up trying to use A_CaretX/Y and used
ControlGet, OutputVar, CurrentLine... and
ControlGet, OutputVar, CurrentCol... instead.
But that's not a general solution.
On my other project I needed GetGUIThreadInfo, and noticed it's capabilities.
So here it is:
Code:
A_Caret(param, coordMode = "Relative")
{
target_window := DllCall("GetForegroundWindow")
If !target_window
Return ""
VarSetCapacity(guiThreadInfo, 48)
NumPut(48, guiThreadInfo, 0)
DllCall("GetGUIThreadInfo", uint, 0, str, guiThreadInfo)
hwndCaret := NumGet(GuiThreadInfo, 28)
If !hwndCaret
Return ""
top := NumGet(guiThreadInfo, 36)
bottom := NumGet(guiThreadInfo, 44)
If param = h
Return bottom - top
left := NumGet(guiThreadInfo, 32)
right := NumGet(guiThreadInfo, 40)
If param = w
Return right - left
VarSetCapacity(sPoint, 8, 0)
NumPut(left, sPoint, 0, "Int")
NumPut(top, sPoint, 4, "Int")
DllCall("ClientToScreen", "UInt", hwndCaret, "UInt", &sPoint)
left := NumGet(sPoint, 0, "Int")
top := NumGet(sPoint, 4, "Int")
If coordMode = Relative
{
VarSetCapacity(rect, 16)
DllCall("GetWindowRect", UInt, target_window, UInt, &rect)
left -= NumGet(Rect, 0, True)
top -= NumGet(Rect, 4, True)
}
If param = x
Return left
Else
Return top
}
Put it in the Lib folder and name it "A_Caret.ahk" to use it as built-in.
The usage is:
A_Caret("X") = caret's horizontal position
A_Caret("Y") = caret's vertical position
A_Caret("W") = caret width (normally 1, higher value for italic text or for a caret customized in Accessibility Options)
A_Caret("H") = caret / line height
A_Caret("X", "Screen") = caret's horizontal position (as CoordMode Caret, Screen)
A_Caret("Y", "Screen") = caret's vertical position (as CoordMode Caret, Screen)
Remember quotes!
Double-click works:
Code:
LButton::
cX := A_Caret("X")
Send {LButton Down}
Return
~LButton Up:: Return
Here's a script that beeps when A_CaretX/Y vs. A_Caret("X"/"Y") have different coordinates (don't use it if you want to double-click). My function returns blank also when there's no hwndCaret, so then it beeps, but otherwise it should beep only occasionally when the caret position or active window chances during ToolTip "evaluation".
Code:
q = "
t := A_Tab
Loop
{
ToolTip % "A_CaretX:" t A_CaretX "`nA_Caret(" q "x" q "):" t A_Caret("x") "`nA_CaretY:" t A_CaretY "`nA_Caret(" q "y" q "):" t A_Caret("y") "`nA_Caret(" q "h" q "):" t A_Caret("h") "`nA_Caret(" q "w" q "):" t A_Caret("w")
If (A_CaretX <> A_Caret("x")) Or (A_CaretY <> A_Caret("y"))
{
SoundBeep, , 20
Sleep 2000
}
Sleep 500
}
Please report possible misbehavior / different coordinates in respect to A_CaretX/Y.