Catching Document Events For ActiveX Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
tqphan
Posts: 26
Joined: 04 Mar 2019, 14:44

Catching Document Events For ActiveX

20 Sep 2021, 21:23

I want to catch OnMouseUp event. I think I need to connect with

Code: Select all

ComObjConnect(WB.Document, WB_events)
but this gives an error of no valid COM object

Code: Select all

MyGui := Gui()
URL := MyGui.Add("Edit", "w930 r1", "https://www.autohotkey.com/boards/")
MyGui.Add("Button", "x+6 yp w44 Default", "Go").OnEvent("Click", ButtonGo)
WB := MyGui.Add("ActiveX", "xm w980 h640", "Shell.Explorer").Value
WB.silent := True
MyGui.Show()
; Continue on to load the initial page:
ButtonGo()
ComObjConnect(WB.Document, WB_events)  ; Connect WB's events to the WB_events class object.

ButtonGo(*) {
    WB.Navigate(URL.Value)
}

class WB_events {
    NavigateComplete2(wb, NewURL) {
        URL.Value := NewURL  ; Update the URL edit control.
    }
    OnMouseUp(doc) {
    	SoundBeep 750, 100
    }
}
safetycar
Posts: 435
Joined: 12 Aug 2017, 04:27

Re: Catching Document Events For ActiveX

22 Sep 2021, 09:42

Seeing that there's no reply here, I've seen 2 things but I think there could be more...
- The event doesn't come from WB.Document but from WB. That part of ComObjConnect should change.
- The way you've defined the WB_events' methods are only accessible after instantiation. To solve that you could create the instance by calling WB_events as WB_events() or if you just wanted to make the methods available without instantiation you will need to use "static" in front of them (static NavigateComplete2...)
User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: Catching Document Events For ActiveX

22 Sep 2021, 19:56

The WB_events methods do not need to be static. The AHK script never directly instantiates this class.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Catching Document Events For ActiveX  Topic is solved

22 Sep 2021, 20:40

they do need to be static if ure gonna be using the class object itself as the event sink, otherwise they wouldnt get called. or u can leave them as non-static instance methods, but then ud actually have to create an instance of the class

Code: Select all

#Requires AutoHotkey v2.0-beta.1

MyGui := Gui()
URL := MyGui.Add("Edit", "w930 r1", "https://www.autohotkey.com/boards/")
MyGui.Add("Button", "x+6 yp w44 Default", "Go").OnEvent("Click", ButtonGo)

WB := MyGui.Add("ActiveX", "xm w980 h640", "Shell.Explorer").Value
WB.silent := True
ComObjConnect(WB, WB_events)  ; Connect WB's events to the WB_events class object.

MyGui.Show()
; Continue on to load the initial page:
ButtonGo()

ButtonGo(*) {
    WB.Navigate(URL.Value)
}

class WB_events {
    static NavigateComplete2(wb, &NewURL, OriginalComWrapper) {
        URL.Value := NewURL  ; Update the URL edit control.

		wb.document.onmouseup := OnMouseUp
	    OnMouseUp() {
	    	SoundBeep 750, 100
	    }
    }
}
tqphan
Posts: 26
Joined: 04 Mar 2019, 14:44

Re: Catching Document Events For ActiveX

23 Sep 2021, 15:05

thank you it works!
tqphan
Posts: 26
Joined: 04 Mar 2019, 14:44

Re: Catching Document Events For ActiveX

23 Sep 2021, 15:20

@swagfag how do i distinguish between the mouse buttons with your method since I don't have the event passed though the OnMouseUp function?
User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: Catching Document Events For ActiveX

23 Sep 2021, 17:20

Hm interesting. Guess that’s just part of the differences due to v2
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Catching Document Events For ActiveX

24 Sep 2021, 20:25

Coco wrote:
13 Jan 2016, 09:05
It seems that the argument(s) are not getting passed unless you specify a reference to a JavaScript function. A solution is to use a JavaScript function as the listener and from there call your AHK function. Something like:

Code: Select all

; Inject JavaScript
window.eval("function eventListener(listener) { return function(e) { return listener(e); }; }")
; You can dynamically specify any AHK function
window.addEventListener("mousemove", window.eventListener(Func("OnMouseMove")))
sounds like what ud classify as a "bug". now only to figure out where - in AHK or in IE. somebody who really, really, reeeeeaallllyyyy likes COM should investigate this
and additionally ull probably have to have applied "the registry fix":
lexikos wrote:
24 Apr 2018, 04:51
"Gui Add, ActiveX" is not something designed for adding HTML to a GUI.
ActiveX components such as the MSIE browser control can be embedded into a GUI window
ActiveX is basically a set of patterns that controls from different developers may follow to allow programs to utilize those controls by calling standard functions. ActiveX has nothing to do with HTML.

You are using the MSIE browser control (a.k.a. the WebBrowser control), which is just one control built following ActiveX patterns. It exists entirely separate from AutoHotkey. AutoHotkey has no knowledge whatsoever of the WebBrowser control; it merely uses an ActiveX function to request the creation of a control from a given string (such as Shell.Explorer or https://autohotkey.com/boards/ as used in the examples). The ActiveX functions look to the registry to find how to create the control.

So the short answer is that the version of ActiveX control you get has nothing to do with the version number of AutoHotkey.

On systems with IE8 or later, the WebBrowser control operates in IE7 compatibility mode by default. Generally the document must contain something like <meta http-equiv="X-UA-Compatible" content="IE=edge"> to override this, unless you use a registry override based on whichever process name you are using (e.g. AutoHotkey.exe).
lexikos wrote:
25 Jan 2016, 03:38
the "IE=edge" should force Shell.Explorer to use the latest IE renderer.
It does. And then you load a different document, and it uses whatever renderer that document appears to need.

If you are displaying HTML which is not under your control, you will need to use the registry fix. Search "FixIE" for details.
lexikos wrote:
24 Jul 2021, 19:10
Internet Explorer 9? :eh: Are you using Vista?

WebBrowser controls operate in IE7-compatibility mode by default, although I'm not sure how that affects the JavaScript environment. There are at least two ways around it:
  • Include <meta http-equiv='X-UA-Compatible' content='IE=edge'> in the document.
  • Use a registry fix (search the forum for FixIE).
lexikos wrote:
26 Dec 2015, 20:29
The WebBrowser control operates in IE7 mode by default. This can only be overridden within the document (with a X-UA-Compatible meta-tag) or via the registry based on your process name. Search the forum for FixIE or FEATURE_BROWSER_EMULATION.
lexikos wrote:
30 May 2015, 22:01
  • window.eval(js) will evaluate js and give you a result, but it seems to fail fairly often if the page is in an older document mode (WB.Document.documentMode). The WebBrowser control operates in IE7 or IE5 mode by default, depending on the !DOCTYPE. You can override it with the FEATURE_BROWSER_EMULATION registry key or putting <meta http-equiv="X-UA-Compatible" content="IE=edge"> in the document.
in other words:

Code: Select all

#Requires AutoHotkey v2.0-beta.1

MyGui := Gui()
URL := MyGui.Add("Edit", "w930 r1", "https://www.autohotkey.com/boards/")
MyGui.Add("Button", "x+6 yp w44 Default", "Go").OnEvent("Click", ButtonGo)

WB := MyGui.Add("ActiveX", "xm w980 h640", "Shell.Explorer").Value
WB.silent := True
ComObjConnect(WB, WB_events)  ; Connect WB's events to the WB_events class object.

MyGui.Show()
; Continue on to load the initial page:
ButtonGo()

ButtonGo(*) {
	WB.Navigate(URL.Value)
}

class WB_events {
	static NavigateComplete2(wb, &NewURL, OriginalComWrapper) {
		URL.Value := NewURL  ; Update the URL edit control.

		doc := wb.document
		win := doc.parentWindow
		win.eval('function jsFuncFromAhkFunc(ahkFunc) { return function(arg1) { return ahkFunc(arg1); }; }')
		doc.addEventListener('mouseup', win.jsFuncFromAhkFunc(OnMouseUp))
		
		OnMouseUp(MouseEvent) { 
			ToolTip(
				'MouseEvent.altKey:'        MouseEvent.altKey                         '`n'
				'MouseEvent.button:'        MouseEvent.button                         '`n'
				'MouseEvent.buttons:'       MouseEvent.buttons                        '`n'
				'MouseEvent.clientX:'       MouseEvent.clientX                        '`n'
				'MouseEvent.clientY:'       MouseEvent.clientY                        '`n'
				'MouseEvent.ctrlKey:'       MouseEvent.ctrlKey                        '`n'
				'MouseEvent.metaKey:'       MouseEvent.metaKey                        '`n'
				'MouseEvent.offsetX:'       MouseEvent.offsetX                        '`n'
				'MouseEvent.offsetY:'       MouseEvent.offsetY                        '`n'
				'MouseEvent.pageX:'         MouseEvent.pageX                          '`n'
				'MouseEvent.pageY:'         MouseEvent.pageY                          '`n'
				'MouseEvent.relatedTarget:' IsObject(MouseEvent.relatedTarget)        '`n'
				'MouseEvent.screenX:'       MouseEvent.screenX                        '`n'
				'MouseEvent.screenY:'       MouseEvent.screenY                        '`n'
				'MouseEvent.shiftKey:'      MouseEvent.shiftKey                       '`n'

				'Modifiers active(pressed/locked)?'                                   '`n'
				'Win:'                      MouseEvent.getModifierState('Win')        '`n'
				'Alt:'                      MouseEvent.getModifierState('Alt')        '`n'
				'AltGraph:'                 MouseEvent.getModifierState('AltGraph')   '`n'
				'CapsLock:'                 MouseEvent.getModifierState('CapsLock')   '`n'
				'Control:'                  MouseEvent.getModifierState('Control')    '`n'
				'Fn:'                       MouseEvent.getModifierState('Fn')         '`n'
				'FnLock:'                   MouseEvent.getModifierState('FnLock')     '`n'
				'Hyper:'                    MouseEvent.getModifierState('Hyper')      '`n'
				'Meta:'                     MouseEvent.getModifierState('Meta')       '`n'
				'NumLock:'                  MouseEvent.getModifierState('NumLock')    '`n'
				'ScrollLock:'               MouseEvent.getModifierState('ScrollLock') '`n'
				'Shift:'                    MouseEvent.getModifierState('Shift')      '`n'
				'Super:'                    MouseEvent.getModifierState('Super')      '`n'
				'Symbol:'                   MouseEvent.getModifierState('Symbol')     '`n'
				'SymbolLock:'               MouseEvent.getModifierState('SymbolLock') '`n'
			)
		}
	}
}
which means u could even move the function back to being a class method, with either:

Code: Select all

	...
	doc.addEventListener('mouseup', win.jsFuncFromAhkFunc(WB_events.OnMouseUp))
}

static OnMouseUp() { 
	; hidden this = MouseEvent
}
...
or

Code: Select all

	...
	doc.addEventListener('mouseup', win.jsFuncFromAhkFunc(WB_events.OnMouseUp.Bind(WB_events)))
}

static OnMouseUp(MouseEvent) { 

}
...

which probably also answers viewtopic.php?p=197613#p197613
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Catching Document Events For ActiveX

25 Sep 2021, 07:39

actually theres also this deprecated method for IE7, so u dont necessarily need "the fix", but ull get less info out of it: https://developer.mozilla.org/en-US/docs/Web/API/Window/event

Code: Select all

#Requires AutoHotkey v2.0-beta.1

MyGui := Gui()
URL := MyGui.Add("Edit", "w930 r1", "https://www.autohotkey.com/boards/")
MyGui.Add("Button", "x+6 yp w44 Default", "Go").OnEvent("Click", ButtonGo)

WB := MyGui.Add("ActiveX", "xm w980 h640", "Shell.Explorer").Value
WB.silent := True
ComObjConnect(WB, WB_events)  ; Connect WB's events to the WB_events class object.

MyGui.Show()
; Continue on to load the initial page:
ButtonGo()

ButtonGo(*) {
	WB.Navigate(URL.Value)
}

class WB_events {
	static NavigateComplete2(wb, &NewURL, OriginalComWrapper) {
		URL.Value := NewURL  ; Update the URL edit control.

		wb.document.onmouseup := OnMouseUp
		OnMouseUp() {
			MouseEvent := wb.document.parentWindow.event
			
			ToolTip(
				'MouseEvent.altKey:'        MouseEvent.altKey                         '`n'
				'MouseEvent.button:'        MouseEvent.button                         '`n'
				; 'MouseEvent.buttons:'       MouseEvent.buttons                        '`n'
				'MouseEvent.clientX:'       MouseEvent.clientX                        '`n'
				'MouseEvent.clientY:'       MouseEvent.clientY                        '`n'
				'MouseEvent.ctrlKey:'       MouseEvent.ctrlKey                        '`n'
				; 'MouseEvent.metaKey:'       MouseEvent.metaKey                        '`n'
				'MouseEvent.offsetX:'       MouseEvent.offsetX                        '`n'
				'MouseEvent.offsetY:'       MouseEvent.offsetY                        '`n'
				; 'MouseEvent.pageX:'         MouseEvent.pageX                          '`n'
				; 'MouseEvent.pageY:'         MouseEvent.pageY                          '`n'
				; 'MouseEvent.relatedTarget:' IsObject(MouseEvent.relatedTarget)        '`n'
				'MouseEvent.screenX:'       MouseEvent.screenX                        '`n'
				'MouseEvent.screenY:'       MouseEvent.screenY                        '`n'
				'MouseEvent.shiftKey:'      MouseEvent.shiftKey                       '`n'

				; 'Modifiers active(pressed/locked)?'                                   '`n'
				; 'Win:'                      MouseEvent.getModifierState('Win')        '`n'
				; 'Alt:'                      MouseEvent.getModifierState('Alt')        '`n'
				; 'AltGraph:'                 MouseEvent.getModifierState('AltGraph')   '`n'
				; 'CapsLock:'                 MouseEvent.getModifierState('CapsLock')   '`n'
				; 'Control:'                  MouseEvent.getModifierState('Control')    '`n'
				; 'Fn:'                       MouseEvent.getModifierState('Fn')         '`n'
				; 'FnLock:'                   MouseEvent.getModifierState('FnLock')     '`n'
				; 'Hyper:'                    MouseEvent.getModifierState('Hyper')      '`n'
				; 'Meta:'                     MouseEvent.getModifierState('Meta')       '`n'
				; 'NumLock:'                  MouseEvent.getModifierState('NumLock')    '`n'
				; 'ScrollLock:'               MouseEvent.getModifierState('ScrollLock') '`n'
				; 'Shift:'                    MouseEvent.getModifierState('Shift')      '`n'
				; 'Super:'                    MouseEvent.getModifierState('Super')      '`n'
				; 'Symbol:'                   MouseEvent.getModifierState('Symbol')     '`n'
				; 'SymbolLock:'               MouseEvent.getModifierState('SymbolLock') '`n'
			)
		}
	}
}

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: songdg, vmech and 21 guests