Detecting if a text is selected

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
afshindavoudy
Posts: 44
Joined: 10 Jan 2024, 13:25

Detecting if a text is selected

Post by afshindavoudy » 06 Feb 2024, 08:43

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

Code: Select all

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?

User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Detecting if a text is selected

Post by mikeyww » 06 Feb 2024, 09:11

This might work but depends on the control.

EditGetSelectedText

afshindavoudy
Posts: 44
Joined: 10 Jan 2024, 13:25

Re: Detecting if a text is selected

Post by afshindavoudy » 06 Feb 2024, 10:10

I am new to ahk and cant fully follow the documents yet!
Can you provide some examples pls.

User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Detecting if a text is selected

Post by mikeyww » 06 Feb 2024, 10:40

A demonstration is below, but it might not work for certain rich-text edit controls.

Code: Select all

#Requires AutoHotkey v2.0
gui1 := Gui()
gui1.AddEdit
gui1.Show

F3:: {
 For ctl in WinGetControls(WinActive('A'))
  If InStr(ctl, 'Edit') {
   Try txt := EditGetSelectedText(ctl)
   Catch
    Continue
   ctlText := 'Control: ' ctl '`n`n'
   If txt = ''
        MsgBox ctlText '[No selection]', 'Selection', 'Icon!'
   Else MsgBox ctlText txt             , 'Selection', 'Iconi'
  }
}

User avatar
Noitalommi_2
Posts: 321
Joined: 16 Aug 2023, 10:58

Re: Detecting if a text is selected

Post by Noitalommi_2 » 06 Feb 2024, 23:27

Hi.

@afshindavoudy
Based on your provided function, you could use OnClipboardChange instead of ClipWait. This way you could avoid the ClipWait timeout, which should speed up the function a little bit if ClipWait times out.

Code: Select all

#Requires AutoHotkey 2.0
#SingleInstance

F1::ToolTip(IsTextSelected())


IsTextSelected() {

    global Result

    SavedClip := ClipboardAll()
    A_Clipboard := ""
    Result := ""

    Send("^c")

    while !Result
        Sleep 10

    r := Result
    A_Clipboard := SavedClip

    if r = "y"
        return 1
    else
        return 0
}
OnClipboardChange ClipChanged
ClipChanged(DataType) {

    global Result

    if DataType = 1 ; 1 = Clipboard contains something that can be expressed as text (this includes files copied from an Explorer window).
        Result := "y"
    else            ; Clipboard contains something entirely non-text such as a picture or is empty
        Result := "n"
}

afshindavoudy
Posts: 44
Joined: 10 Jan 2024, 13:25

Re: Detecting if a text is selected

Post by afshindavoudy » 11 Feb 2024, 05:32

@Noitalommi_2
This script cant detect the selection correctly

User avatar
Noitalommi_2
Posts: 321
Joined: 16 Aug 2023, 10:58

Re: Detecting if a text is selected

Post by Noitalommi_2 » 11 Feb 2024, 07:42

OnClipboardChange considers everything as text that can be expressed as text.
If you want to exclude something then you could check the content of the clipboard beforehand, for example whether it is a path or not if you want to exclude this.
If this is not enough then OnClipboardChange may not be suitable for this task.

Post Reply

Return to “Ask for Help (v2)”