# Perspective text field lose focus on enter press

**URL:** <https://forum.inductiveautomation.com/t/perspective-text-field-lose-focus-on-enter-press/29777>\
**Category:** Ignition\
**Created:** [November 7, 2019, 11:10pm UTC](https://forum.inductiveautomation.com/t/perspective-text-field-lose-focus-on-enter-press/29777 "2019-11-07T23:10:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jonathan](https://avatars.discourse-cdn.com/v4/letter/j/aeb1de/32.png) [@Jonathan](https://forum.inductiveautomation.com/u/Jonathan)\
**Post date:** [November 7, 2019, 11:10pm UTC](https://forum.inductiveautomation.com/t/perspective-text-field-lose-focus-on-enter-press/29777/1 "2019-11-07T23:10:41Z")

</div>

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.

```auto
> 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 = ""

```

---

<div class="post-metadata">

**Author:** ![elliot.lohr](https://avatars.discourse-cdn.com/v4/letter/e/3bc359/32.png) [@elliot.lohr](https://forum.inductiveautomation.com/u/elliot.lohr)\
**Post date:** [July 20, 2020, 1:36pm UTC](https://forum.inductiveautomation.com/t/perspective-text-field-lose-focus-on-enter-press/29777/2 "2020-07-20T13:36:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![code\_skin](https://avatars.discourse-cdn.com/v4/letter/c/43a26b/32.png) [@code\_skin](https://forum.inductiveautomation.com/u/code_skin)\
**Post date:** [July 20, 2020, 1:40pm UTC](https://forum.inductiveautomation.com/t/perspective-text-field-lose-focus-on-enter-press/29777/3 "2020-07-20T13:40:37Z")

</div>

This may be of help to you: [Perspective Component Methods | Ignition User Manual](https://docs.inductiveautomation.com/docs/8.1/ignition-modules/perspective/scripting-in-perspective/perspective-component-methods#requesting-focus)

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

_edit_: updated doc link.

---

<div class="post-metadata">

**Author:** ![elliot.lohr](https://avatars.discourse-cdn.com/v4/letter/e/3bc359/32.png) [@elliot.lohr](https://forum.inductiveautomation.com/u/elliot.lohr)\
**Post date:** [July 20, 2020, 7:45pm UTC](https://forum.inductiveautomation.com/t/perspective-text-field-lose-focus-on-enter-press/29777/4 "2020-07-20T19:45:15Z")

</div>

Thanks, figured it out!

---

<div class="post-metadata">

**Author:** ![Doug\_Goldie](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/doug_goldie/32/58173_2.png) [@Doug\_Goldie](https://forum.inductiveautomation.com/u/Doug_Goldie)\
**Post date:** [July 17, 2025, 2:30pm UTC](https://forum.inductiveautomation.com/t/perspective-text-field-lose-focus-on-enter-press/29777/5 "2025-07-17T14:30:36Z")

</div>

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

---

<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:** [July 17, 2025, 2:46pm UTC](https://forum.inductiveautomation.com/t/perspective-text-field-lose-focus-on-enter-press/29777/6 "2025-07-17T14:46:49Z")

</div>

Hi, Doug. Please see [Wiki - how to post code on this forum](https://forum.inductiveautomation.com/t/wiki-how-to-post-code-on-this-forum/77831). You can then edit your post to fix the code and it will render the indentation and syntax highlighting correctly. Thanks.

---

<div class="post-metadata">

**Author:** ![Doug\_Goldie](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/doug_goldie/32/58173_2.png) [@Doug\_Goldie](https://forum.inductiveautomation.com/u/Doug_Goldie)\
**Post date:** [July 18, 2025, 8:53pm UTC](https://forum.inductiveautomation.com/t/perspective-text-field-lose-focus-on-enter-press/29777/7 "2025-07-18T20:53:58Z")

</div>

```python
# 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!
