# Creating custom event handlers with scripting

**URL:** <https://forum.inductiveautomation.com/t/creating-custom-event-handlers-with-scripting/72009>\
**Category:** Ignition\
**Tags:** vision, ignition81, scripting\
**Created:** [April 4, 2023, 11:45am UTC](https://forum.inductiveautomation.com/t/creating-custom-event-handlers-with-scripting/72009 "2023-04-04T11:45:20Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marek\_Bahnik](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@Marek\_Bahnik](https://forum.inductiveautomation.com/u/Marek_Bahnik)\
**Post date:** [April 4, 2023, 11:45am UTC](https://forum.inductiveautomation.com/t/creating-custom-event-handlers-with-scripting/72009/1 "2023-04-04T11:45:20Z")

</div>

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.

```auto
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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [April 4, 2023, 12:04pm UTC](https://forum.inductiveautomation.com/t/creating-custom-event-handlers-with-scripting/72009/2 "2023-04-04T12:04:00Z")

</div>

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:

> [@Component Event Property Methods](https://forum.inductiveautomation.com/t/component-event-property-methods/30982/2):
>
> The method putClientProperty adds a new property to the component it’s called on. In this instance, @witman was adding a property called singleClick to the button component and setting its value to true. So singleClick was an added custom property. putClientProperty and getClientProperty are methods available to objects that inherit from the javax.swing.JComponent class. The [javadocs](http://files.inductiveautomation.com/sdk/javadoc/ignition79/795/index.html) will show you what methods and properties are available to all Ignition classes, but these all inherit from sta…

---

<div class="post-metadata">

**Author:** ![Marek\_Bahnik](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@Marek\_Bahnik](https://forum.inductiveautomation.com/u/Marek_Bahnik)\
**Post date:** [April 4, 2023, 12:08pm UTC](https://forum.inductiveautomation.com/t/creating-custom-event-handlers-with-scripting/72009/3 "2023-04-04T12:08:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [April 4, 2023, 12:15pm UTC](https://forum.inductiveautomation.com/t/creating-custom-event-handlers-with-scripting/72009/4 "2023-04-04T12:15:15Z")

</div>

> [@Marek\_Bahnik](#):
>
> the _introspect_ script output

You're welcome. 😁

---

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [April 4, 2023, 12:36pm UTC](https://forum.inductiveautomation.com/t/creating-custom-event-handlers-with-scripting/72009/5 "2023-04-04T12:36:07Z")

</div>

> [@Marek\_Bahnik](#):
>
> adding event handlers via scripting

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.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [April 4, 2023, 12:37pm UTC](https://forum.inductiveautomation.com/t/creating-custom-event-handlers-with-scripting/72009/6 "2023-04-04T12:37:57Z")

</div>

> [@justinedwards.jle](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [April 4, 2023, 12:54pm UTC](https://forum.inductiveautomation.com/t/creating-custom-event-handlers-with-scripting/72009/7 "2023-04-04T12:54:38Z")

</div>

Interesting idea:

 ![Screenshot_20230404-075326-251](https://us1.discourse-cdn.com/inductiveautomation/original/3X/2/a/2ad49337f201017a91b58bbc294d6cfbb76b7ef8.png)

---

<div class="post-metadata">

**Author:** ![Marek\_Bahnik](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@Marek\_Bahnik](https://forum.inductiveautomation.com/u/Marek_Bahnik)\
**Post date:** [April 6, 2023, 7:52am UTC](https://forum.inductiveautomation.com/t/creating-custom-event-handlers-with-scripting/72009/8 "2023-04-06T07:52:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Marek\_Bahnik](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@Marek\_Bahnik](https://forum.inductiveautomation.com/u/Marek_Bahnik)\
**Post date:** [April 6, 2023, 11:24am UTC](https://forum.inductiveautomation.com/t/creating-custom-event-handlers-with-scripting/72009/9 "2023-04-06T11:24:44Z")

</div>

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

Using:

```auto
setattr(component, 'mouseReleased', mouseReleased)

```

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

```auto
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?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [April 6, 2023, 12:12pm UTC](https://forum.inductiveautomation.com/t/creating-custom-event-handlers-with-scripting/72009/10 "2023-04-06T12:12:03Z")

</div>

> [@Marek\_Bahnik](#):
>
> Can I work with that in scripting?

Yes. Search for that here in the forums.
