Creating custom event handlers with scripting

So I have been again trying to automate, I have succeeded with tag bindings and custom properties, but what I'm stuck on right now is adding event handlers via scripting.

setattr(component, 'mouseReleased', mouseReleased)

I have been playing with something similar to this, but I'm unsure how to create the mouseReleased callable.

My attempt has been like this:

def mouseReleased():
	param1 = event.source.Path
	window = system.nav.openWindowInstance('Pop Ups/Conveyor Information', {'Tag_Path' : param1})
	system.nav.centerWindow(window)

But I'm not sure if it even is the correct way to add the handler, if the callable is in correct format or if I'm missing something.

One thing what I thought was adding "event" and "button" etc. params to the mouseReleased() signature, but again, I'm not very familiar with the structure.

Thank you for any feedback.

Hmmm. I don't know if those are in the API. You have to set very specificly-named Swing component client properties with lists of objects that encapsulate the events. You might need to do some reflection research on the contents of various client props. Perhaps this topic:

Thank you. I have noticed this segment in the conversation that stood out.

As a side note, you can actually add python function objects themselves to Ignition windows using the putClientProperty which is sometimes useful

Maybe I will give it a go but there is not much documentation regarding this method, I've managed to find it in the introspect script output, so there might be a way.

1 Like

You're welcome. :grin:

2 Likes

I believe there are plenty of examples of this sort of thing in the forum, but the keyword I would use is "listener" instead of event handler.

Those are run-time operations. Marek is scripting creation of configurations that will be saved with the window resource.

1 Like

Interesting idea:

I will have a discussion with a senior colleague so I hope I will get some answers, I will keep this open and keep it updated.

1 Like

So I have done some progress, but right now I feel stuck.

Using:

setattr(component, 'mouseReleased', mouseReleased)

I did manage to set a mouseReleased event on a component to:

def mouseReleased(mouseEvent):
	source = getattr(mouseEvent, "source")
	button = getattr(mouseEvent, "button")
	clickCount = getattr(mouseEvent, "clickCount")
	x = getattr(mouseEvent, "x")
	y = getattr(mouseEvent, "y")
	popupTrigger = getattr(mouseEvent, "popupTrigger")
	altDown = getattr(mouseEvent, "altDown")
	controlDown = getattr(mouseEvent, "controlDown")
	shiftDown = getattr(mouseEvent, "shiftDown")
	
	print introspect.introspect(source)
	print getattr(source, 'dynamicProps')
	
	param1 = getattr(source, 'dynamicProps')
	window = system.nav.openWindowInstance('Pop Ups/Conveyor Information', {'Tag_Path' : param1})
	system.nav.centerWindow(window)

And in Designer it does work, but if you close and reopen the designer it removes the handler and if you launch the project inside the designer it doesn't work.

I suspect there is some another core function inside the component or maybe the window that I don't know of, I didn't find anything more related to mouseReleased in introspect.

Edit: Maybe I need to place the method to mouseListeners property on the component?

Edit 2: Okay so I went through the XML and found the difference, what I'm missing is basically the whole Interaction Controller, which as I understand is a big deal. Can I work with that in scripting?

Yes. Search for that here in the forums.

1 Like