Default Button Event: Press Enter

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!"

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

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.

1 Like

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

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.

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

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...?

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.

Well, these are available for Session:
image

But I see what you're saying about multiple tabs, which is why I asked this question here: Prevent Multiple Tabs

(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.

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.

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

3 Likes

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.

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

2 Likes

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:

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:

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.