# How to set focus in perspective

**URL:** <https://forum.inductiveautomation.com/t/how-to-set-focus-in-perspective/46006>\
**Category:** Ignition\
**Created:** [April 24, 2021, 11:59pm UTC](https://forum.inductiveautomation.com/t/how-to-set-focus-in-perspective/46006 "2021-04-24T23:59:21Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![NII](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nii/32/12920_2.png) [@NII](https://forum.inductiveautomation.com/u/NII)\
**Post date:** [April 24, 2021, 11:59pm UTC](https://forum.inductiveautomation.com/t/how-to-set-focus-in-perspective/46006/1 "2021-04-24T23:59:21Z")

</div>

I have a pop-up view in perspective that contains some labels, buttons and a textfield. I’d like to set the focus to the textfield when the pop-up opens, how can I do this?

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [April 25, 2021, 5:50am UTC](https://forum.inductiveautomation.com/t/how-to-set-focus-in-perspective/46006/2 "2021-04-25T05:50:23Z")

</div>

Right-click the component and select Configure Events. Select the onStartup Event and supply the following code.

```auto
self.focus()

```

---

<div class="post-metadata">

**Author:** ![NII](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nii/32/12920_2.png) [@NII](https://forum.inductiveautomation.com/u/NII)\
**Post date:** [April 25, 2021, 8:01pm UTC](https://forum.inductiveautomation.com/t/how-to-set-focus-in-perspective/46006/3 "2021-04-25T20:01:54Z")

</div>

That’s what I’m looking for, thank you sir.

---

<div class="post-metadata">

**Author:** ![JoeF](https://avatars.discourse-cdn.com/v4/letter/j/4da419/32.png) [@JoeF](https://forum.inductiveautomation.com/u/JoeF)\
**Post date:** [May 25, 2021, 3:58pm UTC](https://forum.inductiveautomation.com/t/how-to-set-focus-in-perspective/46006/4 "2021-05-25T15:58:55Z")

</div>

This method worked while we were on 8.0.12, but seems to have stopped working on 8.0.17. I am working with the same exact scenario as the OP (pop-up window, focus on text field onStartup). Is this fixed in later releases?

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [May 25, 2021, 4:26pm UTC](https://forum.inductiveautomation.com/t/how-to-set-focus-in-perspective/46006/5 "2021-05-25T16:26:34Z")

</div>

Oh, that’s interesting. I just checked in 8.1.6 and it’s not working there _the first time the popup opens_, but it is working on subsequent attempts. We’ll look into this.

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [June 4, 2021, 6:07pm UTC](https://forum.inductiveautomation.com/t/how-to-set-focus-in-perspective/46006/6 "2021-06-04T18:07:14Z")

</div>

I just tried to replicate this again and am unable to replicate the issue in any version after 8.1.6.

---

<div class="post-metadata">

**Author:** ![pkhoshroo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pkhoshroo/32/26801_2.png) [@pkhoshroo](https://forum.inductiveautomation.com/u/pkhoshroo)\
**Post date:** [August 3, 2021, 12:34pm UTC](https://forum.inductiveautomation.com/t/how-to-set-focus-in-perspective/46006/7 "2021-08-03T12:34:01Z")

</div>

Currently, that script is working only for one time.  
For example, if we are using a barcode scanner, when the value is entered automatically, the cursor disappears, and the next barcode can not be entered in the box unless the operator clicks on that box again. How can we write the code to run constantly and not only for one-time use?

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [August 3, 2021, 2:59pm UTC](https://forum.inductiveautomation.com/t/how-to-set-focus-in-perspective/46006/8 "2021-08-03T14:59:07Z")

</div>

> [@pkhoshroo](#):
>
> Currently, that script is working only for one time.

That's expected, because it would only happen when the component gets created.

> [@pkhoshroo](#):
>
> How can we write the code to run constantly and not only for one-time use?

I would use this same approach (the onStartup Event usage) along with a value-changed script. Right-click the value for the `props,text` property of your TextField and select "Add Change Script...". Supply the same code: `self.focus()`. You might also need to set `TextField.props.deferUpdates` to have a value of `false`, and you might need to do the same for `TextField.props.rejectUpdatesWhileFocused`.

---

<div class="post-metadata">

**Author:** ![pkhoshroo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pkhoshroo/32/26801_2.png) [@pkhoshroo](https://forum.inductiveautomation.com/u/pkhoshroo)\
**Post date:** [August 4, 2021, 3:40pm UTC](https://forum.inductiveautomation.com/t/how-to-set-focus-in-perspective/46006/9 "2021-08-04T15:40:03Z")

</div>

> [@cmallonee](#):
>
> deferUpdates

Do we have a similar approach in perspective? I am using version 8.1.6

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [August 4, 2021, 3:58pm UTC](https://forum.inductiveautomation.com/t/how-to-set-focus-in-perspective/46006/10 "2021-08-04T15:58:54Z")

</div>

That recommendation was for Perspective.

---

<div class="post-metadata">

**Author:** ![pkhoshroo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pkhoshroo/32/26801_2.png) [@pkhoshroo](https://forum.inductiveautomation.com/u/pkhoshroo)\
**Post date:** [August 4, 2021, 5:38pm UTC](https://forum.inductiveautomation.com/t/how-to-set-focus-in-perspective/46006/11 "2021-08-04T17:38:44Z")

</div>

> [@cmallonee](#):
>
> deferUpdates

Thank you!  
I was not able to find deferUpdate property and rejectUpdatesWhileFocused. I think this is because I am using numeric field not text field.

[https://docs.inductiveautomation.com/display/DOC80/Perspective+-+Numeric+Entry+Field](https://docs.inductiveautomation.com/display/DOC80/Perspective+-+Numeric+Entry+Field)

And even though, I tried your method for text boxes as well. the cursor remains in that field, which is not what I am looking for. When the operator scan a barcode the cursor should appear at the beginning of the field for the next barcode. Currently, with your method, the new barcode value is added to the previous barcode value,  
so now it is:

barcode 1: 12345  
barcode2: 12345 **98765**

but it should be:  
barcode 1: 12345  
barcode2: **98765**

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [August 4, 2021, 7:15pm UTC](https://forum.inductiveautomation.com/t/how-to-set-focus-in-perspective/46006/12 "2021-08-04T19:15:36Z")

</div>

So then you need to modify the script to point to the “next” barcode. You should look into the [Inductive University](https://inductiveuniversity.com/courses/building-in-perspective) courses for Perspective.  
Instead of `self.focus()`, you should be using something like:

```auto
self.getSibling("NumericEntryField_X").focus()
# modify "_X" to point to the next field

```

---

<div class="post-metadata">

**Author:** ![pkhoshroo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pkhoshroo/32/26801_2.png) [@pkhoshroo](https://forum.inductiveautomation.com/u/pkhoshroo)\
**Post date:** [August 4, 2021, 7:22pm UTC](https://forum.inductiveautomation.com/t/how-to-set-focus-in-perspective/46006/13 "2021-08-04T19:22:26Z")

</div>

Thank you! I know how to point to the next field.  
The application I am trying to do has one field only. By next barcode, I meant the next barcode that the barcode scanner scans.  
So imagine I have one barcode:12345 and its field has a focus on it. when the barcode scanner scans the next barcode, the field should show the next barcode value, for example, 9876, but instead, it shows 123459876 (a combined value!)

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [August 4, 2021, 7:42pm UTC](https://forum.inductiveautomation.com/t/how-to-set-focus-in-perspective/46006/14 "2021-08-04T19:42:45Z")

</div>

As part of your change script, you will need to empty out the field.

```auto
# also do any sort of db write or change logic here before you wipe the value.
self.props.value = ""
self.focus()

```

---

<div class="post-metadata">

**Author:** ![pkhoshroo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pkhoshroo/32/26801_2.png) [@pkhoshroo](https://forum.inductiveautomation.com/u/pkhoshroo)\
**Post date:** [August 4, 2021, 8:18pm UTC](https://forum.inductiveautomation.com/t/how-to-set-focus-in-perspective/46006/15 "2021-08-04T20:18:38Z")

</div>

Thank you! Unfortunately, the new barcode value is still added to the previous one and this is what I did:

First I created an event for that input component:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/6/7/67d89a6fcf775a13e61b5f89e5ca733daf071104.png)

Then I wrote a script for edit property change script:

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/b/7/b79860f172a6e923a89a843d2c838d192cdfd379.png)

And then I assigned those two props to be false:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/8/9/898a644faf045815ab1aa168df04b6dc0d780011.png)

---

<div class="post-metadata">

**Author:** ![jespinmartin1](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jespinmartin1/32/57175_2.png) [@jespinmartin1](https://forum.inductiveautomation.com/u/jespinmartin1)\
**Post date:** [August 4, 2021, 8:39pm UTC](https://forum.inductiveautomation.com/t/how-to-set-focus-in-perspective/46006/16 "2021-08-04T20:39:22Z")

</div>

self.props.value = “” is not valid in your case

---

<div class="post-metadata">

**Author:** ![Edward\_Loomis](https://avatars.discourse-cdn.com/v4/letter/e/90ced4/32.png) [@Edward\_Loomis](https://forum.inductiveautomation.com/u/Edward_Loomis)\
**Post date:** [December 21, 2022, 6:15pm UTC](https://forum.inductiveautomation.com/t/how-to-set-focus-in-perspective/46006/17 "2022-12-21T18:15:09Z")

</div>

I put a binding on the enabled of the text box or numerical control.  
For me it was a tag: [tags]Station0/State  
Transform expression: {value}=110  
Then put a change script on the enabled property of the text box or numerical control.  
It was:  
from time import sleep  
if self.props.enabled:  
sleep(1)   
self.focus()

The issue for me was that I couldn't put the focus on an object while it was changing from a disabled to an enabled state.  
Putting that sleep in there allowed it to change it's focus to itself after it had transitioned to enabled.  
All you have to do is iterate that tag (Station0/State to be 111, in my case) to another value that you have the next box you want to look at. And change the new box to have a binding on enabled looking at this different number.  
(tag: [tags]Station0/State Transform expression: {value}=111)
