Jump to content

Get text (word) under the mouse pointer


Go to solution Solved by PhoenixXL,

Recommended Posts

Posted

Hi,

How to get word under the mouse pointer without selecting it?

Or if it not possible, by selecting it?

I'm trying to get a word under the mouse pointer at a AutoIT's editbox created using GUICtrlCreateEdit

and display the word when the user click on it

Seems cant find the way :(

Thanks!

  • Solution
Posted (edited)

An example

#include <GuiEdit.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $hEdit

Example()

Func Example()

    ; Create GUI
    GUICreate("Edit Char From Pos", 400, 200, -1, -1, $WS_BORDER)
    GUICtrlCreateEdit("Kc and Kp are the equilibrium constants of gaseous mixtures. " & @CRLF & @CRLF & _
            "However, the difference between the two constants is that Kc is defined by molar concentrations," & @CRLF & @CRLF & _
            "whereas Kp is defined by the partial pressures of the gasses inside a closed system." & @CRLF & @CRLF & _
            "The equilibrium constants do not include the concentrations of single components such as liquids and solid, and they do not have any units.", 2, 2, 394, 168, 0)

    $hEdit = GUICtrlGetHandle(-1)
    GUISetState(@SW_SHOW)


    For $i = 1 To 50
        Sleep(500)
        If _WinAPI_GetFocus() = $hEdit Then
            ToolTip(Hover_WordDetect())
        Else
            ToolTip("")
        EndIf
    Next

    GUIDelete()
EndFunc   ;==>Example

Func Hover_WordDetect()

    Local $aCharPos[2], $iMousePos[2]

    $aCharPos = _GUICtrlEdit_CharFromPos($hEdit, _WinAPI_GetMousePosX(True, $hEdit), _WinAPI_GetMousePosY(True, $hEdit))

    Local Const $sLine = _GUICtrlEdit_GetLine($hEdit, $aCharPos[1])
    $aCharPos[0] -= _GUICtrlEdit_LineIndex($hEdit, $aCharPos[1])

    Local $aWords = StringSplit($sLine, " "), $iCount = 0
    For $i = 1 To $aWords[0]
        Switch $aCharPos[0]
            Case $iCount To $iCount + StringLen($aWords[$i])

;~              ConsoleWrite($aWords[$i] & @CRLF)
                Return $aWords[$i]
            Case Else

;~              ConsoleWrite("From: " & $iCount & " to: " & $iCount + StringLen($aWords[$i]) & @TAB & $aCharPos[0] & @CRLF)

                $iCount += StringLen($aWords[$i]) + 1
        EndSwitch
    Next

;~  ConsoleWrite("" & @CRLF)
    Return ""

EndFunc   ;==>Hover_WordDetect

Regards :)

Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Posted

An example

#include <GuiEdit.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $hEdit

Example()

Func Example()

    ; Create GUI
    GUICreate("Edit Char From Pos", 400, 200, -1, -1, $WS_BORDER)
    GUICtrlCreateEdit("Kc and Kp are the equilibrium constants of gaseous mixtures. " & @CRLF & @CRLF & _
            "However, the difference between the two constants is that Kc is defined by molar concentrations," & @CRLF & @CRLF & _
            "whereas Kp is defined by the partial pressures of the gasses inside a closed system." & @CRLF & @CRLF & _
            "The equilibrium constants do not include the concentrations of single components such as liquids and solid, and they do not have any units.", 2, 2, 394, 168, 0)

    $hEdit = GUICtrlGetHandle(-1)
    GUISetState(@SW_SHOW)


    For $i = 1 To 50
        Sleep(500)
        If _WinAPI_GetFocus() = $hEdit Then
            ToolTip(Hover_WordDetect())
        Else
            ToolTip("")
        EndIf
    Next

    GUIDelete()
EndFunc   ;==>Example

Func Hover_WordDetect()

    Local $aCharPos[2], $iMousePos[2]

    $aCharPos = _GUICtrlEdit_CharFromPos($hEdit, _WinAPI_GetMousePosX(True, $hEdit), _WinAPI_GetMousePosY(True, $hEdit))

    Local Const $sLine = _GUICtrlEdit_GetLine($hEdit, $aCharPos[1])
    $aCharPos[0] -= _GUICtrlEdit_LineIndex($hEdit, $aCharPos[1])

    Local $aWords = StringSplit($sLine, " "), $iCount = 0
    For $i = 1 To $aWords[0]
        Switch $aCharPos[0]
            Case $iCount To $iCount + StringLen($aWords[$i])

;~              ConsoleWrite($aWords[$i] & @CRLF)
                Return $aWords[$i]
            Case Else

;~              ConsoleWrite("From: " & $iCount & " to: " & $iCount + StringLen($aWords[$i]) & @TAB & $aCharPos[0] & @CRLF)

                $iCount += StringLen($aWords[$i]) + 1
        EndSwitch
    Next

;~  ConsoleWrite("" & @CRLF)
    Return ""

EndFunc   ;==>Hover_WordDetect

Regards :)

 

Damn, this is so cool, PhoenixXL :thumbsup:

It's 100% accurate when a word is under the mouse pointer, but when we hangover the mouse pointer to some free area (area that just contain blank space),

it still display the word (the latest word?). Any idea how to "fix" this?

Posted

@Bill: thanks, I'm going to see that thread.

@Kidney: I'm trying to create a hyperlink in a editbox.

>My thread seems have no solution. I think if I can detect words under mouse pointer, I can allow user to click on it and my script will open a proper URL.

I'm developing scripts for my company used, not for public

Posted

Damn, this is so cool, PhoenixXL :thumbsup:

It's 100% accurate when a word is under the mouse pointer, but when we hangover the mouse pointer to some free area (area that just contain blank space),

it still display the word (the latest word?). Any idea how to "fix" this?

 

And how to detect if user click on that word?

Posted

And how to detect if user click on that word?

 

I'm using _IsPressed like below, but it's not reliable. Sometime it display the MsgBox, sometime not:

For $i = 1 To 50
        Sleep(500)
        If _WinAPI_GetFocus() = $hEdit Then
            ToolTip(Hover_WordDetect())
            If _IsPressed("01") Then
                msgbox(4096,"Info",Hover_WordDetect())
            EndIf
        Else
            ToolTip("")
        EndIf
Next
Posted

 

I'm using _IsPressed like below, but it's not reliable. Sometime it display the MsgBox, sometime not:

For $i = 1 To 50
        Sleep(500)
        If _WinAPI_GetFocus() = $hEdit Then
            ToolTip(Hover_WordDetect())
            If _IsPressed("01") Then
                msgbox(4096,"Info",Hover_WordDetect())
            EndIf
        Else
            ToolTip("")
        EndIf
Next

 

Ok, for the mouse click part, finally I get it working like this:

For $i = 1 To 50
        Sleep(500)
        If _WinAPI_GetFocus() = $hEdit Then
            ToolTip(Hover_WordDetect())
            If _IsPressed("01")Then
                While _IsPressed("01")
                    $word = Hover_WordDetect()
                    Sleep(10)
                WEnd
                ShellExecute("http://www.google.com/search?q=" & $word)
            EndIf
        EndIf
Next
Posted (edited)

....

It's 100% accurate when a word is under the mouse pointer, but when we hangover the mouse pointer to some free area (area that just contain blank space),

it still display the word (the latest word?). Any idea how to "fix" this?

Formating the text appears to fix "the displaying the latest word while hovering blank areas".

 

Also, I shortened  PhoenixXL's Hover_WordDetect function of post #3.

#include <GuiEdit.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
;#include <Array.au3>

Global $hEdit

Example()

Func Example()

    ; Create GUI
    GUICreate("Edit Char From Pos", 400, 200, -1, -1, $WS_BORDER)
    GUICtrlCreateEdit("Kc and Kp are the equilibrium constants of gaseous mixtures 10" & @CRLF & @CRLF & _
            "However, the difference between the two constants is that Kc is defined by molar concentrations," & @CRLF & @CRLF & _
            "whereas Kp is defined by the partial pressures of the gasses inside a closed system." & @CRLF & @CRLF & _
            "The equilibrium constants do not include the concentrations of single components such as liquids and solid, and they do not have any units.", 2, 2, 394, 168, 0)

    ; --- Format text i.e. add trailing newline, and, insert a space between all fullstops or comas and newlines. -----
    If StringRegExp(GUICtrlRead(-1), "\v+$") = 0 Then GUICtrlSetData(-1, GUICtrlRead(-1) & @CRLF)
    GUICtrlSetData(-1, StringRegExpReplace(GUICtrlRead(-1), "(\V)(\v)", "\1 \2"))
    ; ------------------ End of formatting text ------------------------------------------------

    $hEdit = GUICtrlGetHandle(-1)
    GUISetState(@SW_SHOW)

    For $i = 1 To 50
        Sleep(500)
        If _WinAPI_GetFocus() = $hEdit Then
            ToolTip(Hover_WordDetect())
        Else
            ToolTip("")
        EndIf
    Next
    GUIDelete()
EndFunc   ;==>Example

Func Hover_WordDetect()
    Local $aCharPos[2], $iMousePos[2]
    $aCharPos = _GUICtrlEdit_CharFromPos($hEdit, _WinAPI_GetMousePosX(True, $hEdit), _WinAPI_GetMousePosY(True, $hEdit))
    Local Const $sLine = _GUICtrlEdit_GetLine($hEdit, $aCharPos[1])
    $aCharPos[0] -= _GUICtrlEdit_LineIndex($hEdit, $aCharPos[1])
    ;ConsoleWrite($aCharPos[0] & @LF)
    If StringMid($sLine, $aCharPos[0] + 1, 1) == " " Then
        Return ""
    Else
        Return StringRegExpReplace($sLine, "(?s)^.{0," & $aCharPos[0] & "}(?: |^)([^ ]*).*$", "\1")
    EndIf
EndFunc   ;==>Hover_WordDetect

Edit: Replace the single return "Return StringRegExpReplace($sLine, "(?s)^.{0," & $aCharPos[0] & "}(?: |^)([^ ]*).*$", "1")" with the If...Then...Else. Now when hovering the space between words, Null is returned.

Edit2: Changed RE pattern in "Format text" section from "([,.])(v)" to "(V)(v)":; and,

          Changed 'Return Null' to 'Return "" '.

Edited by Malkey
Posted

So as I understood. You wan't

  • To detect the text that is currently hovered by the mouse(already done)
  • Detect the text when clicked (can be done by using windows messages - WM_LBUTTONDOWN)
  • To have additional check for if the mouse is over blank space (in progress)

The main concept was that the text detected is the one that is highlighted when the user double-click at the certain coordinate.

I can complete the rest of the requirements. Will post back soon.

Regards :)

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Posted

So as I understood. You wan't

  • To detect the text that is currently hovered by the mouse(already done)
  • Detect the text when clicked (can be done by using windows messages - WM_LBUTTONDOWN)
  • To have additional check for if the mouse is over blank space (in progress)

The main concept was that the text detected is the one that is highlighted when the user double-click at the certain coordinate.

I can complete the rest of the requirements. Will post back soon.

Regards :)

 

PhoenixXL, thanks a lot :)

Posted

Formating the text appears to fix "the displaying the latest word while hovering blank areas".

 

Also, I shortened  PhoenixXL's Hover_WordDetect function of post #3.

#include <GuiEdit.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
;#include <Array.au3>

Global $hEdit

Example()

Func Example()

    ; Create GUI
    GUICreate("Edit Char From Pos", 400, 200, -1, -1, $WS_BORDER)
    GUICtrlCreateEdit("Kc and Kp are the equilibrium constants of gaseous mixtures." & @CRLF & @CRLF & _
            "However, the difference between the two constants is that Kc is defined by molar concentrations," & @CRLF & @CRLF & _
            "whereas Kp is defined by the partial pressures of the gasses inside a closed system." & @CRLF & @CRLF & _
            "The equilibrium constants do not include the concentrations of single components such as liquids and solid, and they do not have any units.", 2, 2, 394, 168, 0)

    ; --- Format text i.e. add trailing newline, and, insert a space between all fullstops or comas and newlines. -----
    If StringRegExp(GUICtrlRead(-1), "\v+$") = 0 Then GUICtrlSetData(-1, GUICtrlRead(-1) & @CRLF)
    GUICtrlSetData(-1, StringRegExpReplace(GUICtrlRead(-1), "([,.])(\v)", "\1 \2"))
    ; ------------------ End of formatting text ------------------------------------------------

    $hEdit = GUICtrlGetHandle(-1)
    GUISetState(@SW_SHOW)

    For $i = 1 To 50
        Sleep(500)
        If _WinAPI_GetFocus() = $hEdit Then
            ToolTip(Hover_WordDetect())
        Else
            ToolTip("")
        EndIf
    Next
    GUIDelete()
EndFunc   ;==>Example

Func Hover_WordDetect()
    Local $aCharPos[2], $iMousePos[2]
    $aCharPos = _GUICtrlEdit_CharFromPos($hEdit, _WinAPI_GetMousePosX(True, $hEdit), _WinAPI_GetMousePosY(True, $hEdit))
    Local Const $sLine = _GUICtrlEdit_GetLine($hEdit, $aCharPos[1])
    $aCharPos[0] -= _GUICtrlEdit_LineIndex($hEdit, $aCharPos[1])
    ;ConsoleWrite($aCharPos[0] & @LF)
    If StringMid($sLine, $aCharPos[0] + 1, 1) == " " Then
        Return Null
    Else
        Return StringRegExpReplace($sLine, "(?s)^.{0," & $aCharPos[0] & "}(?: |^)([^ ]*).*$", "\1")
    EndIf
EndFunc   ;==>Hover_WordDetect

Edit: Replace the single return "Return StringRegExpReplace($sLine, "(?s)^.{0," & $aCharPos[0] & "}(?: |^)([^ ]*).*$", "1")" with the If...Then...Else. Now when hovering the space between words, Null is returned.

 

Malkey, thanks a lot :)

Keyword Null is not recognized by 3.8.8.1, could I just replace it with "", so it become Return "" ?

Also, when the string is ended with number, like this:

GUICtrlCreateEdit("Kc and Kp are the equilibrium constants of gaseous mixtures 10" & @CRLF & @CRLF & _
            "However, the difference between the two constants is that Kc is defined by molar concentrations," & @CRLF & @CRLF & _
            "whereas Kp is defined by the partial pressures of the gasses inside a closed system 12" & @CRLF & @CRLF & _
            "The equilibrium constants do not include the concentrations of single components such as liquids and solid, and they do not have any units 13.", 2, 2, 394, 168, 0)

 

It will keep displaying tooltip even the mouse hovering to blank spaces.

Think I should modify the RegExp, but which one and replace to what? o:)

Posted

 

Keyword Null is not recognized by 3.8.8.1

For the keyword to be recognized you need to install the latest stable 3.3.10.x.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Posted

In the example of post #13, see "Edit2"

Changed RE pattern in "Format text" section from "([,.])(v)" to "(V)(v)":; and,

Changed 'Return Null' to 'Return "" '.

Posted

In the example of post #13, see "Edit2"

Changed RE pattern in "Format text" section from "([,.])(v)" to "(V)(v)":; and,

Changed 'Return Null' to 'Return "" '.

 

Great, thanks!

This is another "proof" that I really really need to learn about RegExp :thumbsup:

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...