Sign in to follow this  
Followers 0
NearMadness

How to know when/if text-cursor is within an area

10 posts in this topic

I admit, I've spent hours during the since yesterday on this and need help. It is NearMadness time around here! :)

Ultimately, I need to be able to find out if the user has entered a specific textbox/area in a web form. I am not concerned with HOW he got there (whether it was by tabbing and/or clicking) - just need to know that the text-cursor has entered the object.

For testing purposes, I have tried a number of approaches to determine the coordinates of the text cursor but the results are inconsistent. The code below shows some of my unrealiable tests - just shared to demonstrate effort undertaken but - feel free to ignore it and offer something completely different that actually works!

Ideally, some guru here can offer a Function that can return whether or not the text cursor is within specified coordinates in a specified webform/page/url.

AutoItSetOption("WinTitleMatchMode", 2) ;1=start, 2=subStr, 3=exact, 4=advanced, -1 to -4=Nocase
AutoItSetOption("CaretCoordMode",2) ;2=relative to client window

$varCoreApplWindow = "Contact Us - Online Form Builder | Freedback - Windows Internet Explorer"

$i = 0
While $i <= 10

    If WinExists($varCoreApplWindow) Then
        $state = WinGetState($varCoreApplWindow, "")

        ;2 = Window is visible
        ;4 = Window is enabled
        ;8 = Window is active
        ;16 = Window is minimized
        If BitAnd($state,2) And BitAnd($state,4) And Not BitAnd($state,16) Then
            $a = WinGetCaretPos()
            MsgBox(0,"First Method Pos", $a[0])
            MsgBox(0,"First Method Pos", $a[1])

            $b = _CaretPos()

            MsgBox(0,"Second Method Pos", $b[0])
            MsgBox(0,"Second Method Pos", $b[1])

            ; _Main()
        EndIf
    EndIf

    $i = $i + 1
    MsgBox(0, "Value of $i is:", $i)
WEnd

; If $ans < 0 Then ExitLoop

WinGetCaretPos()


Opt('MustDeclareVars', 1)


Func _Main()
    Local $cursor, $text
    $cursor = _WinAPI_GetCursorInfo()
    $text = "Was the operation sucessful? " & $cursor[0] & @LF
    $text &= "Is the cursor showing? " & $cursor[1] & @LF & @LF
    $text &= "Cursor Handle: " & $cursor[2] & @LF
    $text &= "X Coordinate: " & $cursor[3] & @LF
    $text &= "Y Coordinate: " & $cursor[4]
    MsgBox(0, "_WinApi_GetCursorInfo Example", $text)
EndFunc   ;==>_Main



; More reliable method to get caret coords in MDI text editors.
Func _CaretPos()
    Local $x_adjust =  5
    Local $y_adjust = 40

    Opt("CaretCoordMode", 0)              ;relative mode
    Local $c = WinGetCaretPos()           ;relative caret coords
    Local $w = WinGetPos("")              ;window's coords
    Local $f = ControlGetFocus("","")     ;text region "handle"
    Local $e = ControlGetPos("", "", $f)  ;text region coords

    Local $t[2]
    If IsArray($c) and IsArray($w) and IsArray($e) Then
        $t[0] = $c[0] + $w[0] + $e[0] + $x_adjust
        $t[1] = $c[1] + $w[1] + $e[1] + $y_adjust
        Return $t     ;absolute screen coords of caret cursor
    Else
        SetError(1)
    EndIf
EndFunc

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Can you post the web link for test?... i think controlcommand() can do the job, but i have to figure out how to use it.

Look at the example in the help of the function _IEPropertyGet()

EDITED: Here i extract this.

#include <IE.au3>
$oIE = _IE_Example("form")

$oForm = _IEFormGetObjByName($oIE, "ExampleForm")
$oTextArea = _IEFormElementGetObjByName($oForm, "textareaExample")
$innertext = _IEPropertyGet($oTextArea, "innertext")
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $innertext = ' & $innertext & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
Edited by monoscout999

Share this post


Link to post
Share on other sites

Monoscout,

Thanks for pointing me in the direction of _IEPropertyGet()... I have just begun reading it but can't find a reference to Cursor or Focus... Can you help me a bit more?

Say the webpage is the contact us form at https://www.goodsamdayton.org/gshcontactus.aspx - For instance, how can I find out if the cursor is placed in the Business Phone?

Gracias!

Share this post


Link to post
Share on other sites

i ´m looking into MSDN i think that theres a way to catch some event when the control is active... but i`m new on _IE and objects.. so im learning too. the first problem i have is getting the exact object.

Share this post


Link to post
Share on other sites

MOnoscout,

I've been thinking that _WinAPI_GetCursorInfo holds the answer to this challenge but for some reason it returns unexpected coordinates/results.... although I can see going the way of _IE and objects would be far more reliable and elegant. Thanks for continuing to think about this.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

I advance a little! :) , but still i´m fighting to find out how to catch some Focus event on the object.

Buuuuut ;) i manage to do this "check for changes" script.

#include <IE.au3>
HotKeyset("{esc}","_exit")
$oIE = _IECreate ("https://www.goodsamdayton.org/gshcontactus.aspx")
$oInput = _IEGetObjByName ($oIE, "ctl00$ContentPlaceHolder1$txtBusinessPhone")
;~ for $i = 0 to 10
;~  sleep(100)
;~  $pInput = _IEPropertyset($oInput, "innertext",$i)
;~ Next
; This was a test...
$vInput = _IEFormElementGetValue($oInput) ; I don´t know why  _IEPropertyget($oInput, "innertext") dont work :S
while True
    If _IEFormElementGetValue($oInput) <> $vInput Then
        msgbox(0,"i think i got it","Somethign has changed")
        $vInput = _IEFormElementGetValue($oInput)
    EndIf
    sleep(500)
WEnd

func _exit()
    Exit
EndFunc

Run it, an IE window will open, then try to write something in the Business Phone input.

I have a couple of problems doing this, i dont know why my IE8 cant be attached and always need to open a new window. and i don´t know why _IEPropertyget() dont return anything and i have to use _IEFormElementGetValue(). If someone experienced know any answer i will apreciate.

Edited by monoscout999

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Monoscout999,

You have become a hero around here this morning...! :) That is most excellent progress.

I had not considered comparing strings because I would prefer to intervene right before the user starts typing, however, your solution will do for now --- perhaps it is the only solution. I wonder if the same approach is possible with Firefox...

On the other hand, I had no problems with the following instruction under IE8:

$oIE = _IEAttach ("Good Samaritan")

Thanks again for this excellent code/idea.

Edited by NearMadness

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

i suggest change the thread title to something more understeable like "Focus status of Web object" or "Focus status of web object in FireFox". only a suggest.

#include <IE.au3>
HotKeyset("{esc}","_exit")

$oIE = _IECreate ("https://www.goodsamdayton.org/gshcontactus.aspx")
$oInput = _IEGetObjByName ($oIE, "ctl00$ContentPlaceHolder1$txtBusinessPhone")

while True
    consolewrite($oInput.value&@CRLF)
    sleep(2000)
WEnd

func _exit()
    Exit
EndFunc

this way works too.

EDITED:

yes i got something! ;)

After digging around msdn i get the properties and events list from the input element object, so i solve the way to catch the onfocus event. here you got.

#include <IE.au3>

HotKeyset("{esc}","_exit")

$oIE = _IECreate ("https://www.goodsamdayton.org/gshcontactus.aspx")
$oInput = _IEGetObjByName ($oIE, "ctl00$ContentPlaceHolder1$txtBusinessPhone")
$SinkObject=ObjEvent($oInput,"InputEvent_")

while True
    sleep(2000)
WEnd

func _exit()
    Exit
EndFunc
Func InputEvent_onfocus()
    ConsoleWrite("I got the focus"& @crlf) ;### Debug Console
EndFunc

A couple of links about the object and the event

input type=file Object and onfocus Event

The script open an IE window and get the object by the name, the name is taken from the source of the web page, after that i set the onevent mode from the object, and set the function of onfocus event. This was fun, i never use objects before :)

now im gonna look a way to do it using FireFox...

Edited by monoscout999

Share this post


Link to post
Share on other sites

Your function is PERFECT... That is what I needed!!! Monoscout is now officially elevated to royalty :)

Many thanks! ;)

Share this post


Link to post
Share on other sites

You`re welcome it was fun for me :)

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