When pressing enter have mouse click button

Hello,

I have one input box and one button. Rather then always having to click the button to advance the program I would like to press the enter key. So instead of duplicating the code from the button to the input box I want to use the doClick() function.
My code for the button is under the action mouseClicked and the code for the input box is under keyPressed. This is what I have for the keyPressed event on the input box.

if event.keyCode == event.VK_ENTER:
enterClick = event.source.parent.getComponent(‘submitButton’)
enterClick.doClick()

I would think this should make the code for the submitButton’s mouseClicked event execute when enter is pressed, but it does nothing. You can see the button flash like it is being pressed, but test print statements don’t even execute.

Thank you,
Jonathan

The ActionPerformed event of a button will fire if it gets clicked or pressed with the keyboard (including enter). This should simplify your situation.

As far as your code goes - do you have a tab/spaces to indent the code after the if statement? Where is the doClick() function defined? Typically, we would place this sort of code in a module under the app package.

If you haven’t done so already, I would recommend checking out the FactoryPMI Scripting Videos volume 1 and volume 2.

Jonathan,

You were nearly there! As Nathan pointed out, your code should look like the following:if event.keyCode == event.VK_ENTER: enterClick = event.source.parent.getComponent('submitButton') enterClick.doClick()Tabs are important in Python!

You must also make sure you put your button code in the actionPerformed event of the button.

Nathan, the doClick() function is a standard scripting function for a button.

Thanks for the help. Moving the code from mouseClicked to actionPerformed solved the problem, but now I am stuck on why the value of the numeric text box does not update. it takes two enter key strokes before the value updates.

inputScore->keyPressed:

if event.keyCode == event.VK_ENTER:
    enterClick = event.source.parent.getComponent('submitButton')
    enterClick.doClick()

submitButton->actionPerformed:

print event.source.parent.getComponent('scoreInput').intValue

The initial value of the numeric text box is 0. If I type the number 5 and press enter the number 0 prints. When I press enter again 5 is then printed. If I change the 5 to 8 and press enter 5 prints again.
It appears that the value of the numeric text box does not update until the enter key is pressed twice. Is there some way to force the update of the value.

What I would do is put the code in the propertyChange event of the Numeric Text Field (NTF for short) to respond to the value changing. If the NTF’s Number Type is set to integer, put the following code in the propertyChange event:if event.propertyName == 'intValue': enterClick = event.source.parent.getComponent('submitButton') enterClick.doClick() This will trigger the code attached to the button’s actionPerformed event when the value changes - you will find that it immediately passes the changed value as required.

Note that this can be triggered by pressing the Enter key or by moving the focus from the NTF to something else. If this is a problem, consider setting the Protected mode of the NTF on - this will then require the user to deliberately press the Enter key before entering a new value, which may help avoid inadvertent changes.

The reason that the button is seeing the ‘old’ value of intValue is because you are capturing the VK_ENTER keypress before the Numeric Text Field itself can process that event, and use it to update its value. The fpmi.system.invokeLater function is useful for doing something after the current event is done all processing. Try this:

if event.keyCode == event.VK_ENTER: button = event.source.parent.getComponent('submitButton') def clickIt(button=button): button.doClick() fpmi.system.invokeLater(clickIt)