autohotkeytextselection

Detecting if a text is selected in Autohotkey


I need to check if any text selected in Autohotkey. I have this function which works fine:

IsTextSelected()
{
    SavedClip := ClipboardAll()
    A_Clipboard := ""
    Send("^c")
    Errorlevel := !ClipWait(0.5) ; Wait for the clipboard to contain data
    if (!ErrorLevel) ; There is data on clipboard
        return (true)
    else
        return (false)
    ; Sleep(100)
    A_Clipboard := SavedClip
    SavedClip := ""
    return
}

But its too slow! Is there a way to speed up the process? Or is there another method to achieve that goal?


Solution

  • Here is the correct answer based on Descolada script. (requires UIA.ahk in the same folder as the script)

    #Include UIA.ahk
    IsTextSelected()
    {
        try if (el := UIA.GetFocusedElement()) && el.IsTextPatternAvailable {
            selectionRange := el.GetSelection()[2]
            If selectionRange.GetText()
                return true
            else
                return false
        }
    }