Perspective text field lose focus on enter press

I am trying to make a text field lose focus after the enter key is pressed.

I am trying to use blur(), but it does not seem to work. Also related is setting the text property to empty ("") at the end does not work either, but I think that is because the text field still has focus.

> def runAction(self, event):
> 	"""
> 	Method that will run whenever the selected event fires.
> 
> 	Arguments:
> 		self: A reference to the component that is invoking this function.
> 		event: Events fired by the relevant keyboard interaction.
> 			altKey (bool): True if the 'alt' key was held down when the event
> 			               was fired.
> 			charCode (int | float): Deprecated.
> 			ctrlKey (bool): True if the 'ctrl' key was held down when the event
> 			                was fired.
> 			key (str): The value of the key interacted with in this event.
> 			keyCode (int | float): Deprecated.
> 			locale (str): The locale of the keyboard in use on the device. May
> 			              be blank.
> 			location (int | float): The location of the key interacted with on
> 			                        the keyboard.
> 			metaKey (bool): True if the 'meta' key was held down when the event
> 			                was fired.
> 			repeat (bool): True if the key is being held down such that it
> 			               automatically repeats.
> 			shiftKey (bool): True if the 'shift' key was held down when the
> 			                 event was fired.
> 			which (int | float): Deprecated.
> 	"""
> 	if(event.key == "Enter"):
> 		system.perspective.sendMessage("userResponse", payload=self.props.text, scope="page")
> 		
> 		self.blur()
> 		
> 		self.props.text = ""

Did you ever have any luck getting the text field to lose focus? Trying to do something similar

This may be of help to you: Perspective Component Methods | Ignition User Manual

You could use another component to take focus away from the component you're trying to lose it from.

edit: updated doc link.

1 Like

Thanks, figured it out!

Edit Oops, I realised after posting this post is for Perspective and my solution is for Vision; not sure if the scripting works the same, but can be useful for a Vision Client, to be sure!

I've been using this in the "Key Pressed" event handler scripting:

Check if the pressed key is the desired one (e.g., Enter key)

if event.keyCode == event.VK_ENTER: # Replace VK_ENTER with the desired key constant
# Transfer focus to another component or clear focus
event.source.parent.getRootPane().requestFocusInWindow() # Moves focus to the parent container

If you click a numeric entry component, it highlights and lets you type. As soon as you press enter OR click out of the component, focus is lost. Works well so you don't enter a setpoint, hit enter, then keep typing something else and accidentally rewrite your setpoint because focus was not lost on pressing enter!

Hi, Doug. Please see Wiki - how to post code on this forum. You can then edit your post to fix the code and it will render the indentation and syntax highlighting correctly. Thanks.

# Check if the pressed key is the desired one (e.g., Enter key)

if event.keyCode == event.VK_ENTER: # Replace VK_ENTER with the desired key constant
# Transfer focus to another component or clear focus
event.source.parent.getRootPane().requestFocusInWindow() # Moves focus to the parent container

If you click a numeric entry component, it highlights and lets you type. As soon as you press enter OR click out of the component, focus is lost. Works well so you don't enter a setpoint, hit enter, then keep typing something else and accidentally rewrite your setpoint because focus was not lost on pressing enter!