Sign in to follow this  
Followers 0
michaelslamet

Get text (word) under the mouse pointer

25 posts in this topic

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!

Share this post


Link to post
Share on other sites



#3 ·  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
1 person likes this

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.

Share this post


Link to post
Share on other sites

lol very cool. just curious as to how this is useful?

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

@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

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Would a rich edit control not be better suited?

I imagine you can have hyperlinks in them.

 

Yes, you're correct. Rich edit control should be more suitable, but for this time I need a editbox, for a reason explain >here :)

Share this post


Link to post
Share on other sites

 

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

Share this post


Link to post
Share on other sites

#13 ·  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
1 person likes this

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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 :)

Share this post


Link to post
Share on other sites

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:)

Share this post


Link to post
Share on other sites

 

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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

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

 

Yes, for now I am still in love very much with 3.8.8.1 :P

Share this post


Link to post
Share on other sites

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 "" '.

Share this post


Link to post
Share on other sites

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:

Share this post


Link to post
Share on other sites

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
Sign in to follow this  
Followers 0