Jump to content

ObjEvent usage.


milos83
 Share

Go to solution Solved by trancexx,

Recommended Posts

Hu guys,

Regarding ObjEvent usage on IE (IE.au3).

If I use

ObjEvent(_IEGetObjById($oIE, "btn1"), "_MyBTN_", "HTMLAnchorEvents2")

Func _MyBTN_onclick()

    ConsoleWrite("Clicked" & @LF)

Endfunc

to listen for a click on a element, how can I retrieve its attributes.

For example ID, outertext, or Value ?

If yes, can I do ObjEvent on a body and then catch all elements clicked and parse them in a loop?

This would allow me to use only one function for all the elements.

Thanks

Edited by milos83
Link to comment
Share on other sites

Hi John.

Why are you so sure?

I am not quite familiar with this but as I can see that once set, ObjEvent can pass a lot of parameters. Like in the example below.

Local $oIEEvents = ObjEvent($oIE, "_IEEvent_", "DWebBrowserEvents2"); BeforeNavigate2 method definition

Func _IEEvent_BeforeNavigate2($IEpDisp, $IEURL, $IEFlags, $IETargetFrameName, $IEPostData, $IEHeaders, $IECancel)
    ConsoleWrite("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!--BeforeNavigate2 fired--!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! " & @CRLF & _
            "$IEpDisp = " & $IEpDisp() & "  -  " & ObjName($IEpDisp) & @CRLF & _ ; e.g. default property and name for the object
            "$IEURL = " & $IEURL & @CRLF & _
            "$IEFlags = " & $IEFlags & @CRLF & _
            "$IETargetFrameName = " & $IETargetFrameName & @CRLF & _
            "$IEPostData = " & $IEPostData & @CRLF & _
            "$IEHeaders = " & $IEHeaders & @CRLF & _
            "$IECancel = " & $IECancel & @CRLF & _
            "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! " & @CRLF & @CRLF)
EndFunc   ;==>_IEEvent_BeforeNavigate2

I googled a lot about this. MS site has some info, but where do I find what parameters will ObjEvent send to the function?

 

In javascript you can receive element ID like this:

onclick="MyIdIs(this.id)"

Thanks

BTW. Are you The John?

Edited by milos83
Link to comment
Share on other sites

Of course you can attach event handler:

#include <IE.au3>

Local $oIE = _IECreate("http://google.com")
Local $oButton = _IEGetObjById ($oIE, "gbqfba") ; search button (probably)

ConsoleWrite(ObjName($oButton) & @CRLF)
ConsoleWrite(ObjName($oButton, 4) & @CRLF) ; File with Interface defs
ConsoleWrite(ObjName($oButton, 6) & @CRLF) ; CLSID of the object's coclass

; Event object
Local $oEvent = ObjEvent($oButton, "_MY_EVENT_HANDLER_")

Sleep(10000)

_IEQuit($oIE)


Func _MY_EVENT_HANDLER_onclick($oEvtObj)
    ConsoleWrite("!!!!!CLICKED THE BUTTON" & @CRLF)
    MsgBox(4096, "Yay JohnOne!", "You clicked the button.")
EndFunc 
If you look at that code and see what it prints you will get the name of the object, file with interface definition and CLSID of the object's coclass.

Then all you have to do is find script called TLBViewer.au3 in the examples forum and load it with the file that you got (and then save data to some text file) and then find CLSID and look for which event interfaces it implements (it will be something like "// Implemented interface: <IDispatch> HTMLButtonElementEvents" and whatnot. Then find definition of that interface by scrolling up or down an read what events you can catch.

Edited by trancexx

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

Thanks Transexx.

That looks really interesting, but it seems I am lost...

==================================================================================

coclass HTMLAnchorElement;
CLSID = {3050F248-98B5-11CF-BB82-00AA00BDCE0B};

// Implemented interface: <IDispatch> DispHTMLAnchorElement
// Implemented interface: <IDispatch> HTMLAnchorEvents
// Implemented interface: <IDispatch> HTMLAnchorEvents2
// Implemented interface: <IDispatch> IHTMLElement
// Implemented interface: <IDispatch> IHTMLElement2
// Implemented interface: <IDispatch> IHTMLElement3
// Implemented interface: <IDispatch> IHTMLElement4
// Implemented interface: <IDispatch> IHTMLUniqueName
// Implemented interface: <IDispatch> IHTMLDOMNode
// Implemented interface: <IDispatch> IHTMLDOMNode2

==================================================================================

Tried all of them and none worked

ObjEvent($oButton, "_MY_EVENT_HANDLER_", "IHTMLDOMNode")

This is a small part whats above

 

    471.
STDCALL PROPERTYGET DISPATCH;
         BSTR ie8_shape(
         );

    472.
STDCALL PROPERTYPUTREF DISPATCH;
         VOID ie8_coords(
             BSTR v
         );

    473.
STDCALL PROPERTYGET DISPATCH;
         BSTR ie8_coords(
         );

    474.
STDCALL PROPERTYPUTREF DISPATCH;
         VOID ie8_href(
             BSTR v
         );

    475.
STDCALL PROPERTYGET DISPATCH;
         BSTR ie8_href(
         );

and below

IDispatch HTMLLabelEvents2;
IID = {3050F61C-98B5-11CF-BB82-00AA00BDCE0B};
// Inherits from:  IDispatch  {00020400-0000-0000-C000-000000000046}

    1.
STDCALL FUNC DISPATCH;
         bool onhelp(
            [in] <IDispatch> IHTMLEventObj* pEvtObj
         );

    2.
STDCALL FUNC DISPATCH;
         bool onclick(
            [in] <IDispatch> IHTMLEventObj* pEvtObj
         );

    3.
STDCALL FUNC DISPATCH;
         bool ondblclick(
            [in] <IDispatch> IHTMLEventObj* pEvtObj
         );

    4.
STDCALL FUNC DISPATCH;
         bool onkeypress(
            [in] <IDispatch> IHTMLEventObj* pEvtObj
         );

What exactly am I looking for?

Thanks

Link to comment
Share on other sites

Actually @trancexx

I wanna make sure we understand each other.

Is it possible to do this kind of thing?

ObjEvent($oButton1, "_MY_EVENT_HANDLER_")
ObjEvent($oButton2, "_MY_EVENT_HANDLER_") ;<-- button 2 same handler

func _MY_EVENT_HANDLER_OnClick($objID)
      ConsoleWrite("Button clicked is" & $objID)
Endfunc

or if I can get an object and then do $objID.id

 

If its too complicated I'll quit from this concept and I'll do it with each object own handler.

Let me know what you think.

Edited by milos83
Link to comment
Share on other sites

similar topic

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 
Link to comment
Share on other sites

It's not about whether it's complicated or not, it's about how it should be done.

That other thing is how it should be done:

Local $sHtml = '<body> <a id="idLink" href="#" title="Link" >Click me</a> <br><br> <button id="idButton" title="Button" >Click me</button> </body>'

Local $oIE = ObjCreate("InternetExplorer.Application")
$oIE.navigate("about:blank")
Sleep(200) ; lame
$oIE.document.write($sHtml)
$oIE.visible = True

Local $oDoc = $oIE.document
ConsoleWrite(@error & @CRLF)
Local $oLink1 = $oIE.document.getElementById("idLink")
Local $oButton2 = $oIE.document.getElementById("idButton")

; Event objects
Local $oEventBrowser = ObjEvent($oIE, "Browser_", "DWebBrowserEvents2")
Local $oEventLink1 = ObjEvent($oLink1, "Link1_")
Local $oEventButton2 = ObjEvent($oButton2, "Button2_")

;-----------------------------------------------
While Sleep(100)
WEnd
; THE END
;-----------------------------------------------


;Events for browser
Func Browser_OnQuit()
    Exit
EndFunc



;  Events for link
Func Link1_onclick($oEvtObj)
    $oIE.document.getElementById("idLink").innerhtml = "You clicked me"
EndFunc

Func Link1_onmouseover($oEvtObj)
    $oIE.document.getElementById("idLink").innerhtml = "Hovering"
EndFunc

Func Link1_onmouseout($oEvtObj)
    $oIE.document.getElementById("idLink").innerhtml = "Not Hovering"
EndFunc



; Events for button
Func Button2_onmouseover($oEvtObj)
    $oIE.document.getElementById("idButton").innerhtml = "Hovering Button (click to Exit)"
EndFunc

Func Button2_onmouseout($oEvtObj)
    $oIE.document.getElementById("idButton").innerhtml = "Not Hovering Button (click to Exit)"
EndFunc

Func Button2_onclick($oEvtObj)
    $oIE.Quit()
    Exit
EndFunc
Or even better:

Global $oIE = ObjCreate("InternetExplorer.Application")
Local $oEventBrowser = ObjEvent($oIE, "Browser_", "DWebBrowserEvents2")
Local $oEventLink1, $oEventButton2 ; to collect event objects to

$oIE.navigate("about:blank")


;-----------------------------------------------
While Sleep(100)
WEnd
; THE END
;-----------------------------------------------

Func Customize($oBrowser, ByRef $oEventLink1, ByRef $oEventButton2)
    Local $sHtml = '<body> <a id="idLink" href="#" title="Link" >Click me</a> <br><br> <button id="idButton" title="Button" >Click me</button> </body>'
    Local $oDoc = $oBrowser.document
    $oDoc.write($sHtml)

    $oBrowser.visible = True

    Local $oLink1 = $oDoc.getElementById("idLink")
    Local $oButton2 = $oDoc.getElementById("idButton")

    ; Event objects
    $oEventLink1 = ObjEvent($oLink1, "Link1_")
    $oEventButton2 = ObjEvent($oButton2, "Button2_")
EndFunc


;Events for browser
Func Browser_OnQuit()
    Exit
EndFunc

Func Browser_DocumentComplete($oDisp, $sUrl)
    Customize($oDisp, $oEventLink1, $oEventButton2)
EndFunc


;  Events for link
Func Link1_onclick($oEvtObj)
    $oIE.document.getElementById("idLink").innerhtml = "You clicked me"
EndFunc

Func Link1_onmouseover($oEvtObj)
    $oIE.document.getElementById("idLink").innerhtml = "Hovering"
EndFunc

Func Link1_onmouseout($oEvtObj)
    $oIE.document.getElementById("idLink").innerhtml = "Not Hovering"
EndFunc



; Events for button
Func Button2_onmouseover($oEvtObj)
    $oIE.document.getElementById("idButton").innerhtml = "Hovering Button (click to Exit)"
EndFunc

Func Button2_onmouseout($oEvtObj)
    $oIE.document.getElementById("idButton").innerhtml = "Not Hovering Button (click to Exit)"
EndFunc

Func Button2_onclick($oEvtObj)
    $oIE.Quit()
    Exit
EndFunc
Edited by trancexx

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

It's not about whether it's complicated or not, it's about how it should be done.

That other thing is how it should be done:

Local $sHtml = '<body> <a id="idLink" href="#" title="Link" >Click me</a> <br><br> <button id="idButton" title="Button" >Click me</button> </body>'

Local $oIE = ObjCreate("InternetExplorer.Application")
$oIE.navigate("about:blank")
Sleep(200) ; lame
$oIE.document.write($sHtml)
$oIE.visible = True

Local $oDoc = $oIE.document
ConsoleWrite(@error & @CRLF)
Local $oLink1 = $oIE.document.getElementById("idLink")
Local $oButton2 = $oIE.document.getElementById("idButton")

; Event objects
Local $oEventBrowser = ObjEvent($oIE, "Browser_", "DWebBrowserEvents2")
Local $oEventLink1 = ObjEvent($oLink1, "Link1_")
Local $oEventButton2 = ObjEvent($oButton2, "Button2_")

;-----------------------------------------------
While Sleep(100)
WEnd
; THE END
;-----------------------------------------------


;Events for browser
Func Browser_OnQuit()
    Exit
EndFunc



;  Events for link
Func Link1_onclick($oEvtObj)
    $oIE.document.getElementById("idLink").innerhtml = "You clicked me"
EndFunc

Func Link1_onmouseover($oEvtObj)
    $oIE.document.getElementById("idLink").innerhtml = "Hovering"
EndFunc

Func Link1_onmouseout($oEvtObj)
    $oIE.document.getElementById("idLink").innerhtml = "Not Hovering"
EndFunc



; Events for button
Func Button2_onmouseover($oEvtObj)
    $oIE.document.getElementById("idButton").innerhtml = "Hovering Button (click to Exit)"
EndFunc

Func Button2_onmouseout($oEvtObj)
    $oIE.document.getElementById("idButton").innerhtml = "Not Hovering Button (click to Exit)"
EndFunc

Func Button2_onclick($oEvtObj)
    $oIE.Quit()
    Exit
EndFunc
Or even better:

Global $oIE = ObjCreate("InternetExplorer.Application")
Local $oEventBrowser = ObjEvent($oIE, "Browser_", "DWebBrowserEvents2")
Local $oEventLink1, $oEventButton2 ; to collect event objects to

$oIE.navigate("about:blank")


;-----------------------------------------------
While Sleep(100)
WEnd
; THE END
;-----------------------------------------------

Func Customize($oBrowser, ByRef $oEventLink1, ByRef $oEventButton2)
    Local $sHtml = '<body> <a id="idLink" href="#" title="Link" >Click me</a> <br><br> <button id="idButton" title="Button" >Click me</button> </body>'
    Local $oDoc = $oBrowser.document
    $oDoc.write($sHtml)

    $oBrowser.visible = True

    Local $oLink1 = $oDoc.getElementById("idLink")
    Local $oButton2 = $oDoc.getElementById("idButton")

    ; Event objects
    $oEventLink1 = ObjEvent($oLink1, "Link1_")
    $oEventButton2 = ObjEvent($oButton2, "Button2_")
EndFunc


;Events for browser
Func Browser_OnQuit()
    Exit
EndFunc

Func Browser_DocumentComplete($oDisp, $sUrl)
    Customize($oDisp, $oEventLink1, $oEventButton2)
EndFunc


;  Events for link
Func Link1_onclick($oEvtObj)
    $oIE.document.getElementById("idLink").innerhtml = "You clicked me"
EndFunc

Func Link1_onmouseover($oEvtObj)
    $oIE.document.getElementById("idLink").innerhtml = "Hovering"
EndFunc

Func Link1_onmouseout($oEvtObj)
    $oIE.document.getElementById("idLink").innerhtml = "Not Hovering"
EndFunc



; Events for button
Func Button2_onmouseover($oEvtObj)
    $oIE.document.getElementById("idButton").innerhtml = "Hovering Button (click to Exit)"
EndFunc

Func Button2_onmouseout($oEvtObj)
    $oIE.document.getElementById("idButton").innerhtml = "Not Hovering Button (click to Exit)"
EndFunc

Func Button2_onclick($oEvtObj)
    $oIE.Quit()
    Exit
EndFunc

 

@Trancexx Thank you, but it seems like we still don't understand each other.

 

Look here, this is the part of my code before I started this thread:

Global $oIEEvents = ObjEvent(_IEGetObjById($oIE, "btn1"), "_MyBTN1_", "HTMLAnchorEvents2")
Global $oIEEvents = ObjEvent(_IEGetObjById($oIE, "btn2"), "_MyBTN2_", "HTMLAnchorEvents2")
Global $oIEEvents = ObjEvent(_IEGetObjById($oIE, "btn3"), "_MyBTN3_", "HTMLAnchorEvents2")
Global $oIEEvents = ObjEvent(_IEGetObjById($oIE, "btn4"), "_MyBTN4_", "HTMLAnchorEvents2")
Global $oIEEvents = ObjEvent(_IEGetObjById($oIE, "btn5"), "_MyBTN5_", "HTMLAnchorEvents2")
Global $oIEEvents = ObjEvent(_IEGetObjById($oIE, "btn6"), "_MyBTN6_", "HTMLAnchorEvents2")
Global $oIEEvents = ObjEvent(_IEGetObjById($oIE, "btn7"), "_MyBTN7_", "HTMLAnchorEvents2")
Global $oIEEvents = ObjEvent(_IEGetObjById($oIE, "btn8"), "_MyBTN8_", "HTMLAnchorEvents2")
Global $oIEEvents = ObjEvent(_IEGetObjById($oIE, "btn9"), "_MyBTN9_", "HTMLAnchorEvents2")


Func _MyBTN1_onclick()

W("btn1")

EndFunc   ;==>_MyLink_onclick

Func _MyBTN2_onclick()

W("btn2")

EndFunc   ;==>_MyLink_onclick 

and so on for every button

And it works, that's not the problem.

Now, my goal is to avoid making a function for EVERY button. In other words, How to receive Clicked Element ID directly on W() function.

In simple words, How can I get an ID for a clicked element without pre knowing its id (like in your example).

Thank you for your time and effort.

Edited by milos83
Link to comment
Share on other sites

  • Solution

Meh, I forgot that you can make event functions execute synchronously. LOL and I wrote the code for that.

Yes, you can get id or object that fired the event from within event function:

Global $oIE = ObjCreate("InternetExplorer.Application")
Local $oEventBrowser = ObjEvent($oIE, "Browser_", "DWebBrowserEvents2")
Local $oEventLink1, $oEventButton2 ; to collect event objects to

$oIE.navigate("about:blank")


;-----------------------------------------------
While Sleep(100)
WEnd
; THE END
;-----------------------------------------------

Func Customize($oBrowser, ByRef $oEventLink1, ByRef $oEventButton2)
    Local $sHtml = '<body> <a id="idLink" href="#" title="Link" >Click me</a> <br><br> <button id="idButton" title="Button" >Click me</button> </body>'
    Local $oDoc = $oBrowser.document
    $oDoc.write($sHtml)

    $oBrowser.visible = True

    Local $oLink1 = $oDoc.getElementById("idLink")
    Local $oButton2 = $oDoc.getElementById("idButton")

    ; Event objects
    $oEventLink1 = ObjEvent($oLink1, "Event_", "HTMLAnchorEvents2")
    $oEventButton2 = ObjEvent($oButton2, "Event_", "HTMLButtonElementEvents2")
EndFunc


;Events for browser
Func Browser_OnQuit()
    Exit
EndFunc

Func Browser_DocumentComplete($oDisp, $sUrl)
    Customize($oDisp, $oEventLink1, $oEventButton2)
EndFunc



; Events
volatile Func Event_onmouseover($oEvtObj)
    If IsObj($oEvtObj) Then
        ConsoleWrite("id = " & $oEvtObj.srcElement.id & " fired onmouseover" & @CRLF)
        Switch $oEvtObj.srcElement
            Case $oIE.document.getElementById("idLink")
                $oEvtObj.srcElement.innerhtml = "Hovering Anchor"
            Case $oIE.document.getElementById("idButton")
                $oEvtObj.srcElement.innerhtml = "Hovering Button (click to Exit)"
        EndSwitch
    EndIf
EndFunc

volatile Func Event_onmouseout($oEvtObj)
    If IsObj($oEvtObj) Then
        ConsoleWrite("id = " & $oEvtObj.srcElement.id & " fired onmouseout" & @CRLF)
        Switch $oEvtObj.srcElement ; check
            Case $oIE.document.getElementById("idLink")
                $oEvtObj.srcElement.innerhtml = "Not Hovering Anchor"
            Case $oIE.document.getElementById("idButton")
                $oEvtObj.srcElement.innerhtml = "Not Hovering Button (click to Exit)"
        EndSwitch
    EndIf
EndFunc

volatile Func Event_onclick($oEvtObj)
    If IsObj($oEvtObj) Then
        ConsoleWrite("id = " & $oEvtObj.srcElement.id & " fired onclick" & @CRLF)
        If $oEvtObj.srcElement = $oIE.document.getElementById("idButton") Then
            $oIE.Quit()
            Exit
        EndIf
    EndIf
EndFunc

Then It has to be HTMLAnchorEvents2, HTMLButtonElementEvents2, etc...

You can compare id-s or objects directly to get the one that fired (i'm checking objects in that example).

Edited by trancexx

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

Meh, I forgot that you can make event functions execute synchronously. LOL and I wrote the code for that.

Yes, you can get id or object that fired the event from within event function:

Global $oIE = ObjCreate("InternetExplorer.Application")
Local $oEventBrowser = ObjEvent($oIE, "Browser_", "DWebBrowserEvents2")
Local $oEventLink1, $oEventButton2 ; to collect event objects to

$oIE.navigate("about:blank")


;-----------------------------------------------
While Sleep(100)
WEnd
; THE END
;-----------------------------------------------

Func Customize($oBrowser, ByRef $oEventLink1, ByRef $oEventButton2)
    Local $sHtml = '<body> <a id="idLink" href="#" title="Link" >Click me</a> <br><br> <button id="idButton" title="Button" >Click me</button> </body>'
    Local $oDoc = $oBrowser.document
    $oDoc.write($sHtml)

    $oBrowser.visible = True

    Local $oLink1 = $oDoc.getElementById("idLink")
    Local $oButton2 = $oDoc.getElementById("idButton")

    ; Event objects
    $oEventLink1 = ObjEvent($oLink1, "Event_", "HTMLAnchorEvents2")
    $oEventButton2 = ObjEvent($oButton2, "Event_", "HTMLButtonElementEvents2")
EndFunc


;Events for browser
Func Browser_OnQuit()
    Exit
EndFunc

Func Browser_DocumentComplete($oDisp, $sUrl)
    Customize($oDisp, $oEventLink1, $oEventButton2)
EndFunc



; Events
volatile Func Event_onmouseover($oEvtObj)
    If IsObj($oEvtObj) Then
        ConsoleWrite("id = " & $oEvtObj.srcElement.id & " fired onmouseover" & @CRLF)
        Switch $oEvtObj.srcElement
            Case $oIE.document.getElementById("idLink")
                $oEvtObj.srcElement.innerhtml = "Hovering Anchor"
            Case $oIE.document.getElementById("idButton")
                $oEvtObj.srcElement.innerhtml = "Hovering Button (click to Exit)"
        EndSwitch
    EndIf
EndFunc

volatile Func Event_onmouseout($oEvtObj)
    If IsObj($oEvtObj) Then
        ConsoleWrite("id = " & $oEvtObj.srcElement.id & " fired onmouseout" & @CRLF)
        Switch $oEvtObj.srcElement ; check
            Case $oIE.document.getElementById("idLink")
                $oEvtObj.srcElement.innerhtml = "Not Hovering Anchor"
            Case $oIE.document.getElementById("idButton")
                $oEvtObj.srcElement.innerhtml = "Not Hovering Button (click to Exit)"
        EndSwitch
    EndIf
EndFunc

volatile Func Event_onclick($oEvtObj)
    If IsObj($oEvtObj) Then
        ConsoleWrite("id = " & $oEvtObj.srcElement.id & " fired onclick" & @CRLF)
        If $oEvtObj.srcElement = $oIE.document.getElementById("idButton") Then
            $oIE.Quit()
            Exit
        EndIf
    EndIf
EndFunc

Then It has to be HTMLAnchorEvents2, HTMLButtonElementEvents2, etc...

You can compare id-s or objects directly to get the one that fired (i'm checking objects in that example).

volatile ?

Link to comment
Share on other sites

newer seen that one but...

http://www.autoitscript.com/autoit3/docs/keywords/Volatile.htm

Edited by bogQ

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 
Link to comment
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
 Share

  • Recently Browsing   0 members

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