# Adding a newly made method to a keyListener

**URL:** <https://forum.servoy.com/t/adding-a-newly-made-method-to-a-keylistener/20226>\
**Category:** Questions\
**Created:** [April 20, 2020, 1:16pm UTC](https://forum.servoy.com/t/adding-a-newly-made-method-to-a-keylistener/20226 "2020-04-20T13:16:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mees](https://avatars.discourse-cdn.com/v4/letter/m/f1d935/32.png) [@mees](https://forum.servoy.com/u/mees)\
**Post date:** [April 20, 2020, 1:16pm UTC](https://forum.servoy.com/t/adding-a-newly-made-method-to-a-keylistener/20226/1 "2020-04-20T13:16:34Z")

</div>

Hi all,

I am currently making a function that creates keylisteners and callbacks on the fly, and adds them to an element. I’ve got it all working, except for one thing: I cannot get the addKeyListener method to recognize my new method correctly. The code to do so seems to be quite simple:

```auto
			/** @type {JSMethod} */
			var method = solutionForm.newMethod(methodCode);
			plugins.keyListener.addKeyListener('keyListener_' + elementName, method, true);

```

The first call creates a function called onKeyTxtTest for me. This works properly. It’s the second line that doesn’t function propertly. If I use the code

```auto
			/** @type {JSMethod} */
			var method = solutionForm.newMethod(splitCode.join('\n'));
			plugins.keyListener.addKeyListener('keyListener_' + elementName, onKeyTxtTest, true);

```

everything works as intended. So how do i get the addKeyListener-function to recognize my method? I tried

```auto
			plugins.keyListener.addKeyListener('keyListener_' + elementName, method.getUUID(), true);
			plugins.keyListener.addKeyListener('keyListener_' + elementName, method.getName(), true);

```

But both to no avail.

---

<div class="post-metadata">

**Author:** ![mboegem](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/mboegem/32/4399_2.png) [@mboegem](https://forum.servoy.com/u/mboegem)\
**Post date:** [April 20, 2020, 3:14pm UTC](https://forum.servoy.com/t/adding-a-newly-made-method-to-a-keylistener/20226/2 "2020-04-20T15:14:47Z")

</div>

Hi Mees,

the addKeyListener function expects the callbackFunction as a reference to the function.

So if you created the method on form ‘myForm’ this should work:

```auto
plugins.keyListener.addKeyListener('keyListener_' + elementName, forms.myForm[method.getName()], true);

```

Hope that helps.

---

<div class="post-metadata">

**Author:** ![mees](https://avatars.discourse-cdn.com/v4/letter/m/f1d935/32.png) [@mees](https://forum.servoy.com/u/mees)\
**Post date:** [April 21, 2020, 6:23am UTC](https://forum.servoy.com/t/adding-a-newly-made-method-to-a-keylistener/20226/3 "2020-04-21T06:23:43Z")

</div>

That did the trick, thank you very much Marc!
