# Default Button Event: Press Enter

**URL:** <https://forum.inductiveautomation.com/t/default-button-event-press-enter/76500>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [July 18, 2023, 1:18am UTC](https://forum.inductiveautomation.com/t/default-button-event-press-enter/76500 "2023-07-18T01:18:21Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![mdemaris](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mdemaris/32/54639_2.png) [@mdemaris](https://forum.inductiveautomation.com/u/mdemaris)\
**Post date:** [July 18, 2023, 1:18am UTC](https://forum.inductiveautomation.com/t/default-button-event-press-enter/76500/1 "2023-07-18T01:18:21Z")

</div>

Question:  
I want to set a button to receive pressing "Enter" as the default, so I don't have to click on the button.

How do I do that? I don't see any props that say "Hey, if you press the Enter key my `onActionPerformed` event will fire!"

---

<div class="post-metadata">

**Author:** ![MTekin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mtekin/32/50257_2.png) [@MTekin](https://forum.inductiveautomation.com/u/MTekin)\
**Post date:** [July 26, 2023, 10:44pm UTC](https://forum.inductiveautomation.com/t/default-button-event-press-enter/76500/2 "2023-07-26T22:44:58Z")

</div>

Hi @mdemaris,

Instead of using onActionPerformed, you can try to use the onKeyDown event that has the property "key" which is the value of the key pressed and "keycode" which is a unicode representation of that key. This page has more information on the key event properties: [Perspective Event Types Reference - Ignition User Manual 8.1 - Ignition Documentation](https://docs.inductiveautomation.com/display/DOC81/Perspective+Event+Types+Reference#PerspectiveEventTypesReference-KeyboardEventProperties)

One thing to note about the onKeyDown event is that the focus has to be on the component with the event when the key is pressed. So, if you had it on the button, then users would need to mouse over the button before pressing enter. So, it might be better to have it on a larger component. This won't work on the root container.

---

<div class="post-metadata">

**Author:** ![mdemaris](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mdemaris/32/54639_2.png) [@mdemaris](https://forum.inductiveautomation.com/u/mdemaris)\
**Post date:** [July 26, 2023, 10:58pm UTC](https://forum.inductiveautomation.com/t/default-button-event-press-enter/76500/3 "2023-07-26T22:58:13Z")

</div>

There is no way, like many normal web pages, to set a button with a default "Enter" key press?

Unless... those other web pages work as you describe, the "enter key" action is not attached to the button, per se, but a larger container...

---

<div class="post-metadata">

**Author:** ![victordcq](https://avatars.discourse-cdn.com/v4/letter/v/f6c823/32.png) [@victordcq](https://forum.inductiveautomation.com/u/victordcq)\
**Post date:** [July 27, 2023, 6:48am UTC](https://forum.inductiveautomation.com/t/default-button-event-press-enter/76500/4 "2023-07-27T06:48:24Z")

</div>

you can do an onfocus on the button on view startup. that way the button will be focused (unless they click something else)

a website would also just listen to any press on the bigest wrapper.

---

<div class="post-metadata">

**Author:** ![mdemaris](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mdemaris/32/54639_2.png) [@mdemaris](https://forum.inductiveautomation.com/u/mdemaris)\
**Post date:** [September 27, 2023, 12:33am UTC](https://forum.inductiveautomation.com/t/default-button-event-press-enter/76500/5 "2023-09-27T00:33:32Z")

</div>

Is it possible to call the event on the button from this key down event on the view container?

---

<div class="post-metadata">

**Author:** ![mdemaris](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mdemaris/32/54639_2.png) [@mdemaris](https://forum.inductiveautomation.com/u/mdemaris)\
**Post date:** [November 16, 2023, 6:57pm UTC](https://forum.inductiveautomation.com/t/default-button-event-press-enter/76500/6 "2023-11-16T18:57:19Z")

</div>

Would it be possible to create a Session Event, that would handle the Key Stroke, (_if certain views are open_) and somehow activate the code to capture the values, send them to the named query so the user can see the returned dataset?

Or maybe a project level thing that is listening for the Key Stroke for certain views...?

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [November 16, 2023, 7:15pm UTC](https://forum.inductiveautomation.com/t/default-button-event-press-enter/76500/7 "2023-11-16T19:15:12Z")

</div>

> Would it be possible to create a Session Event, that would handle the Key Stroke,

Keypress, I think, would not be a session event. (Think multiple tabs open on the same Perspective application.) It would have to be a view event.

> ... and somehow activate the code to capture the values, send them to the named query ...

What has a keypress got to do with a named query? It's just an event that can be used to trigger something else.

> Or maybe a project level thing that is listening for the Key Stroke for certain views..

How would this work? If it were possible it would have to handle embedded views, repeaters, docks, etc.

---

<div class="post-metadata">

**Author:** ![mdemaris](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mdemaris/32/54639_2.png) [@mdemaris](https://forum.inductiveautomation.com/u/mdemaris)\
**Post date:** [November 16, 2023, 10:18pm UTC](https://forum.inductiveautomation.com/t/default-button-event-press-enter/76500/8 "2023-11-16T22:18:04Z")

</div>

Well, these are available for Session:  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/e/d/eddc485f889af879a05425925d9090aa6645af40.png)

But I see what you're saying about multiple tabs, which is why I asked this question here: [Prevent Multiple Tabs](https://forum.inductiveautomation.com/t/how-do-i-prevent-multiple-pages-with-the-same-url/80560)

(I haven't worked on the tab issue since the last post, as there were other things to deal with.)

The issue I have with the Enter key is that I have to press it twice to get the "action" to work if I input values into the numeric input fields. If I use the text input boxes, I only need to press Enter once.

> [@victordcq](#):
>
> a website would also just listen to any press on the bigest wrapper.

In this case, the biggest wrapper is the root, and that is where the `onKeyDown` event resides.

A colleague asked about a higher level function/listener, i.e. Project/Session, that might be able to solve this, which is why I asked.

---

<div class="post-metadata">

**Author:** ![victordcq](https://avatars.discourse-cdn.com/v4/letter/v/f6c823/32.png) [@victordcq](https://forum.inductiveautomation.com/u/victordcq)\
**Post date:** [November 17, 2023, 7:57am UTC](https://forum.inductiveautomation.com/t/default-button-event-press-enter/76500/9 "2023-11-17T07:57:54Z")

</div>

> [@mdemaris](#):
>
> The issue I have with the Enter key is that I have to press it twice to get the "action" to work if I input values into the numeric input fields. If I use the text input boxes, I only need to press Enter once.

Why is that?  
listening on the key events on both these works the same?

Anyways i think you are to stuck on wanting to trigger the button. You dont need to trigger the button. You just need to trigger a script, so put the script that is in the button inside your projecet script and call this script from the button and from your onKey events.  
Or use a messagehandler with scope view and trigger this messagehandler from the button the same way as the keyevents

---

<div class="post-metadata">

**Author:** ![mdemaris](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mdemaris/32/54639_2.png) [@mdemaris](https://forum.inductiveautomation.com/u/mdemaris)\
**Post date:** [November 20, 2023, 11:56pm UTC](https://forum.inductiveautomation.com/t/default-button-event-press-enter/76500/10 "2023-11-20T23:56:32Z")

</div>

Well, I'm not sure why I have to press Enter twice. I understand that pressing Enter does not activate the Button.

The button has the script, as well as the `onKey` event script.

I did place a `change script` in one of the numeric boxes, so that pressing enter allows the focus(?) to move off the numeric box and triggers the action script.

---

<div class="post-metadata">

**Author:** ![victordcq](https://avatars.discourse-cdn.com/v4/letter/v/f6c823/32.png) [@victordcq](https://forum.inductiveautomation.com/u/victordcq)\
**Post date:** [November 21, 2023, 7:29am UTC](https://forum.inductiveautomation.com/t/default-button-event-press-enter/76500/11 "2023-11-21T07:29:33Z")

</div>

Dont move the focus, move the script to a general position (project scripts) and let both the inputfield and the button trigger the same script  
[https://docs.inductiveautomation.com/display/DOC81/Project+Library#ProjectLibrary-AddaScript](https://docs.inductiveautomation.com/display/DOC81/Project+Library#ProjectLibrary-AddaScript)

---

<div class="post-metadata">

**Author:** ![mdemaris](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mdemaris/32/54639_2.png) [@mdemaris](https://forum.inductiveautomation.com/u/mdemaris)\
**Post date:** [November 21, 2023, 7:27pm UTC](https://forum.inductiveautomation.com/t/default-button-event-press-enter/76500/12 "2023-11-21T19:27:33Z")

</div>

I like the project script idea, and it works perfectly. However, it does not solve the issue with the numeric input fields and having to press Enter twice. I think it has something to do with the way this component works, @cmallonee mentioned something about it's behavior in another post, if I find it I'll link it. (Actually, he was talking about the text field and numeric box about changing focus.)

Here is the script:

```auto
def SearchFunc(catalog, master, partNum, desc, category, area):
	query = 'INV/ItemSearchFilter'
	params = {"p_area":area, "p_cat":catalog, "p_mast":master, "p_partNum":partNum, "p_desc":desc, "p_category":category}
	
	return system.db.runNamedQuery(query, params)

```

Here is the `onKey` code:

```auto
def runAction(self, event):
	if event.key == 'Enter':
		
		catalog = self.view.custom.search_data.catalog
		master = self.view.custom.search_data.master
		partNum = self.view.custom.search_data.partNum
		desc = self.view.custom.search_data.desc
		category = self.view.custom.search_data.category
		area = self.view.custom.search_data.area
	
# namedQuery = "INV/ItemSearchFilter"
# params = {"p_area":area, "p_cat":catalog, "p_mast":master, "p_partNum":partNum, "p_desc":desc, "p_category":category}
		self.custom.table_data = Test_SearchPart_01.SearchFunc(catalog, master, partNum, desc, category, area)

```

All other actions to run this code have been disabled.

Other than that, it works fine.
