# Simulate a "button press" event using the enter key, no matter what is in focus

**URL:** <https://forum.inductiveautomation.com/t/simulate-a-button-press-event-using-the-enter-key-no-matter-what-is-in-focus/80462>\
**Category:** Ignition\
**Tags:** vision\
**Created:** [October 13, 2023, 4:44pm UTC](https://forum.inductiveautomation.com/t/simulate-a-button-press-event-using-the-enter-key-no-matter-what-is-in-focus/80462 "2023-10-13T16:44:03Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bushmatic](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bushmatic/32/87014_2.png) [@Bushmatic](https://forum.inductiveautomation.com/u/Bushmatic)\
**Post date:** [October 13, 2023, 4:44pm UTC](https://forum.inductiveautomation.com/t/simulate-a-button-press-event-using-the-enter-key-no-matter-what-is-in-focus/80462/1 "2023-10-13T16:44:03Z")

</div>

Another question for the forum! I have a simple Vision window containing a couple text input boxes and a button. The button has an event handler script on "action performed."

Users are telling me they wish to be able to hit the 'Enter' key on their keyboards in order to fire the button instead of clicking it manually. This is after they have manually typed some text into the input boxes beforehand -- not important for this question.

I am wondering if I can trigger an "action performed" event on the button without actually performing the action? Like if I put a script on the text input boxes (or the root container itself?) to watch for the enter key and then fire a pseudo "action performed" on the button. Possible?

---

<div class="post-metadata">

**Author:** ![franklepkowski](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/franklepkowski/32/4808_2.png) [@franklepkowski](https://forum.inductiveautomation.com/u/franklepkowski)\
**Post date:** [October 13, 2023, 4:50pm UTC](https://forum.inductiveautomation.com/t/simulate-a-button-press-event-using-the-enter-key-no-matter-what-is-in-focus/80462/2 "2023-10-13T16:50:56Z")

</div>

Have a look at this.

> [@Identifier for 'Enter' with onKeyPress event for v8.1.5](https://forum.inductiveautomation.com/t/identifier-for-enter-with-onkeypress-event-for-v8-1-5/48853):
>
> I am trying to change some parameters using the onKeyPress event with a text field using the enter key as the trigger to make the updates. This worked fine in v8.0.xx. After updating to 8.1.5, it appears that when enter is pressed it returns an empty string or at least that is what it looks like. Below are the if statements that I have attempted to return True for but have not been able to. if event.key == 'Enter' or event.Key == 'enter': if event.key == '': if event.key == ' ': Example output…

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [October 13, 2023, 5:19pm UTC](https://forum.inductiveautomation.com/t/simulate-a-button-press-event-using-the-enter-key-no-matter-what-is-in-focus/80462/3 "2023-10-13T17:19:32Z")

</div>

I would consider moving the button script to a project script, then calling it from wherever you need.

---

<div class="post-metadata">

**Author:** ![Bushmatic](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bushmatic/32/87014_2.png) [@Bushmatic](https://forum.inductiveautomation.com/u/Bushmatic)\
**Post date:** [October 13, 2023, 5:29pm UTC](https://forum.inductiveautomation.com/t/simulate-a-button-press-event-using-the-enter-key-no-matter-what-is-in-focus/80462/4 "2023-10-13T17:29:49Z")

</div>

I like this idea. I think I'll go this route. Thank you! And thanks Frank as well.

---

<div class="post-metadata">

**Author:** ![Bushmatic](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bushmatic/32/87014_2.png) [@Bushmatic](https://forum.inductiveautomation.com/u/Bushmatic)\
**Post date:** [October 13, 2023, 8:33pm UTC](https://forum.inductiveautomation.com/t/simulate-a-button-press-event-using-the-enter-key-no-matter-what-is-in-focus/80462/5 "2023-10-13T20:33:30Z")

</div>

I was able to get this working as I wanted by doing what Jordan said -- I moved the button's script into a shared project script, then passed the parameters into this script from various other places on the Vision window, including the original button in question.

For the "enter key" logic, I simply put a "keyPressed" event handler script on each editable text field. Once I had one text field working, I could copy it across to the other ones. The working code starts as follows:

```auto
from java.awt.event import KeyEvent
if event.keyCode == KeyEvent.VK_ENTER:

```

I'm very happy with the way the project is running now.

---

<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:** [October 13, 2023, 8:44pm UTC](https://forum.inductiveautomation.com/t/simulate-a-button-press-event-using-the-enter-key-no-matter-what-is-in-focus/80462/6 "2023-10-13T20:44:23Z")

</div>

I avoid using import in events. Consider doing this in your library script:

```auto
from java.awt.event.KeyEvent import VK_ENTER

```

Then your event script can do this:

```auto
if event.keyCode == someScript.VK_ENTER:

```

---

<div class="post-metadata">

**Author:** ![Bushmatic](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bushmatic/32/87014_2.png) [@Bushmatic](https://forum.inductiveautomation.com/u/Bushmatic)\
**Post date:** [October 13, 2023, 8:57pm UTC](https://forum.inductiveautomation.com/t/simulate-a-button-press-event-using-the-enter-key-no-matter-what-is-in-focus/80462/7 "2023-10-13T20:57:24Z")

</div>

Interesting, I didn't realize you could do it like that. Makes a lot of sense though. I've moved the `import` command to the shared script. I just had to put it above the main `def` area before it would work the same as before. Thank you!

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [October 13, 2023, 9:01pm UTC](https://forum.inductiveautomation.com/t/simulate-a-button-press-event-using-the-enter-key-no-matter-what-is-in-focus/80462/8 "2023-10-13T21:01:58Z")

</div>

VK\_ENTER is part of the event, as well. No import needed:

```auto
if event.keyCode == event.VK_ENTER:

```

---

<div class="post-metadata">

**Author:** ![Bushmatic](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bushmatic/32/87014_2.png) [@Bushmatic](https://forum.inductiveautomation.com/u/Bushmatic)\
**Post date:** [October 13, 2023, 9:13pm UTC](https://forum.inductiveautomation.com/t/simulate-a-button-press-event-using-the-enter-key-no-matter-what-is-in-focus/80462/9 "2023-10-13T21:13:12Z")

</div>

Nice, you're right! I've made all the recommended updates and everything is still working great and is decidedly more robust. Appreciate all the help.

Edit: I had to replace `if event.keyCode == event.VK_ENTER:` with `if event.keyCode == 10:` because I realized the code was not working with the numpad's enter key -- only the "normal" enter key. Changing the code to the above solved this issue.

---

<div class="post-metadata">

**Author:** ![Miguel\_Villasenor\_Es](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/miguel_villasenor_es/32/60416_2.png) [@Miguel\_Villasenor\_Es](https://forum.inductiveautomation.com/u/Miguel_Villasenor_Es)\
**Post date:** [March 7, 2024, 5:29pm UTC](https://forum.inductiveautomation.com/t/simulate-a-button-press-event-using-the-enter-key-no-matter-what-is-in-focus/80462/10 "2024-03-07T17:29:59Z")

</div>

Am I doing something wrong? I can't get the script to work. I'll get an error that says something to the affect of " event has no attribute VK\_ENTER".

````auto
	# IF ENTER IS HIT WE RUN THE SCRIPT
	if event.keyCode == event.VK_ENTER:
	
		# VALIDATING USER INPUT SO WE CAN RUN A PROPPER QUERY
		if self.getSibling("TextField_Asset").props.text is not None and len(self.getSibling("TextField_Asset").props.text) > 3:  									     
			txt = "%" + self.getSibling("TextField_Asset").props.text + "%" # CREATING LIKE PARAMETER %userinput%
			self.custom.custpro = txt # WE HAVE A CUSTOM PROPERTY ON THE BUTTON, IT'S A PARAMATER THAT'S BOUND TO THE TABLE
			query = "SELECT COUNT(*) FROM ProductionData.dbo.DrawingInfo WHERE Val1 LIKE ? AND DocType = 2"				      
			results = system.db.runPrepQuery(query, [txt]) # RUNNING QUERY		   
			value = results.getValueAt(0,0)
			self.getSibling("Label_Just_Doc_Found").props.text = "Documents Found: " + str(value) # UPDATING THE NUMBER OF DOCUMENTS FOUND		    
			    ```
**I've also tried :**

````

if event.keyCode == '\n':

```auto

```

if event.keyCode == 10:

```auto

```

---

<div class="post-metadata">

**Author:** ![Bushmatic](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bushmatic/32/87014_2.png) [@Bushmatic](https://forum.inductiveautomation.com/u/Bushmatic)\
**Post date:** [March 7, 2024, 8:57pm UTC](https://forum.inductiveautomation.com/t/simulate-a-button-press-event-using-the-enter-key-no-matter-what-is-in-focus/80462/11 "2024-03-07T20:57:24Z")

</div>

Here is a snippet of my working code if it helps anything. Make sure you have your script on a keyPressed event in the first place.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/1/e/1ef9780db433c0c886af1248a1ec89266a3f6445.png)

---

<div class="post-metadata">

**Author:** ![Brandon\_Peterson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/brandon_peterson/32/32960_2.png) [@Brandon\_Peterson](https://forum.inductiveautomation.com/u/Brandon_Peterson)\
**Post date:** [March 7, 2024, 9:42pm UTC](https://forum.inductiveautomation.com/t/simulate-a-button-press-event-using-the-enter-key-no-matter-what-is-in-focus/80462/12 "2024-03-07T21:42:56Z")

</div>

In Vision, there's this nifty property you can add to a button that allows the user to press enter when focused anywhere in the window\* and cause the button to be pressed. I haven't used it before, but it looks exactly like what this thread is trying to accomplish. I would use it primarily for `[Submit]`, `[Okay]`, or `[Close]` buttons on a popup window.

> **Default Button** : If true, this button will be activated when the user presses Enter on the window.  
> `.defaultBtn`  
> [Vision - Button | Ignition User Manual](https://www.docs.inductiveautomation.com/docs/8.1/appendix/components/vision-components/buttons/button#properties)

\*_The docs say "on the window", but I'm not sure what happens if a component inside the window is focused instead of / in addition to the window itself. If anyone else knows, I'm curious too!_

---

<div class="post-metadata">

**Author:** ![Bushmatic](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bushmatic/32/87014_2.png) [@Bushmatic](https://forum.inductiveautomation.com/u/Bushmatic)\
**Post date:** [March 7, 2024, 9:45pm UTC](https://forum.inductiveautomation.com/t/simulate-a-button-press-event-using-the-enter-key-no-matter-what-is-in-focus/80462/13 "2024-03-07T21:45:54Z")

</div>

Yes! Miguel, you should just do what Brandon is saying here. I remember learning about this in my core training course, several months after I wrote the keyPressed event stuff, and thinking how much easier that would have been 😂

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [March 8, 2024, 12:05pm UTC](https://forum.inductiveautomation.com/t/simulate-a-button-press-event-using-the-enter-key-no-matter-what-is-in-focus/80462/14 "2024-03-08T12:05:15Z")

</div>

> [@Miguel\_Villasenor\_Es](#):
>
> Am I doing something wrong?

You're trying to use something in Perspective that was meant for Vision.

---

<div class="post-metadata">

**Author:** ![Miguel\_Villasenor\_Es](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/miguel_villasenor_es/32/60416_2.png) [@Miguel\_Villasenor\_Es](https://forum.inductiveautomation.com/u/Miguel_Villasenor_Es)\
**Post date:** [March 11, 2024, 4:41pm UTC](https://forum.inductiveautomation.com/t/simulate-a-button-press-event-using-the-enter-key-no-matter-what-is-in-focus/80462/15 "2024-03-11T16:41:38Z")

</div>

I looked around for a bit and finally found the solution.

```auto
if event.charCode == 13:
	# SOME CODE

```

Guess you can do what I wanted in perspective and the solution to this isn't moving the button script to a project script.
