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

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?

Have a look at this.

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

3 Likes

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

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:

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

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

1 Like

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

from java.awt.event.KeyEvent import VK_ENTER

Then your event script can do this:

if event.keyCode == someScript.VK_ENTER:
1 Like

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!

VK_ENTER is part of the event, as well. No import needed:

if event.keyCode == event.VK_ENTER:
4 Likes

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.

1 Like

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

	# 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':


if event.keyCode == 10:

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.

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

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

1 Like

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 :joy:

1 Like

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

5 Likes

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

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.

3 Likes