检测是否在 Autohotkey 中选择了文本

问题描述 投票:0回答:2

我需要检查在 Autohotkey 中是否选择了任何文本。 我有这个功能,效果很好:

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
}

但是太慢了! 有没有办法加快这个过程? 或者还有其他方法可以实现这个目标吗?

autohotkey textselection
2个回答
0
投票

这是基于 Decolada 脚本的正确答案。 (需要 UIA.ahk 与脚本位于同一文件夹中)

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

0
投票

不知道是否可以更快地满足您的需要,但也许......

F1::
    RetainedClip := ClipboardAll
    Clipboard = 
        Send, ^c
    If Clipboard = 
        MsgBox, , , No text selected., 3
    Else
        MsgBox, , , Text is selected., 3
        Clipboard := RetainedClip
Return
© www.soinside.com 2019 - 2024. All rights reserved.