# Perspective focus with selected text

**URL:** <https://forum.inductiveautomation.com/t/perspective-focus-with-selected-text/83853>\
**Category:** General Discussion\
**Tags:** ignition81, perspective\
**Created:** [January 5, 2024, 6:09pm UTC](https://forum.inductiveautomation.com/t/perspective-focus-with-selected-text/83853 "2024-01-05T18:09:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bwirth](https://avatars.discourse-cdn.com/v4/letter/b/41988e/32.png) [@bwirth](https://forum.inductiveautomation.com/u/bwirth)\
**Post date:** [January 5, 2024, 6:09pm UTC](https://forum.inductiveautomation.com/t/perspective-focus-with-selected-text/83853/1 "2024-01-05T18:09:03Z")

</div>

I have built a control panel for a client, and I can select focus of a numeric entry field, but what they would like is that it is focused with the number selected as when you click on a number with the mouse (as shown). Is there a way to take focus with the value selected? (version 8.1.32)

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/8/0/8056c6a64fcff845f9d1f1d4ee55a3cebf2c203d.png)

---

<div class="post-metadata">

**Author:** ![schenk](https://avatars.discourse-cdn.com/v4/letter/s/6bbea6/32.png) [@schenk](https://forum.inductiveautomation.com/u/schenk)\
**Post date:** [January 5, 2024, 6:59pm UTC](https://forum.inductiveautomation.com/t/perspective-focus-with-selected-text/83853/2 "2024-01-05T18:59:14Z")

</div>

If I'm understanding your question correctly, try setting `props.mode` to `direct` on the numeric entry

---

<div class="post-metadata">

**Author:** ![bwirth](https://avatars.discourse-cdn.com/v4/letter/b/41988e/32.png) [@bwirth](https://forum.inductiveautomation.com/u/bwirth)\
**Post date:** [January 5, 2024, 7:15pm UTC](https://forum.inductiveautomation.com/t/perspective-focus-with-selected-text/83853/3 "2024-01-05T19:15:45Z")

</div>

Thank you, that is the way I have it.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/2/5/257cc5087b5a67483d829050fd0e5c4cebe66c90.png)

---

<div class="post-metadata">

**Author:** ![bwirth](https://avatars.discourse-cdn.com/v4/letter/b/41988e/32.png) [@bwirth](https://forum.inductiveautomation.com/u/bwirth)\
**Post date:** [January 5, 2024, 7:25pm UTC](https://forum.inductiveautomation.com/t/perspective-focus-with-selected-text/83853/4 "2024-01-05T19:25:46Z")

</div>

Think I have it. I have self.focus() on the startup event for that field. It is performing that prior to reading the value I'm placing into the field. I put a short sleep prior to the focus event and it is now working as expected. Question would be is there a better way?

```auto
def runAction(self):
	import time
	time.sleep(.3)
	self.focus()

```

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [January 5, 2024, 7:58pm UTC](https://forum.inductiveautomation.com/t/perspective-focus-with-selected-text/83853/5 "2024-01-05T19:58:43Z")

</div>

> [@bwirth](#):
>
> `time.sleep(.3)`

Don't get in this habit. There is almost certainly a better way. I'm not sure what that is, but I would be very cautious about where I sleep anything on a UI thread (even though this is Perspective).

---

<div class="post-metadata">

**Author:** ![djs](https://avatars.discourse-cdn.com/v4/letter/d/e47774/32.png) [@djs](https://forum.inductiveautomation.com/u/djs)\
**Post date:** [January 5, 2024, 8:36pm UTC](https://forum.inductiveautomation.com/t/perspective-focus-with-selected-text/83853/6 "2024-01-05T20:36:07Z")

</div>

You could put your `self.focus()` in a valueChange script on the value prop of the numeric entry field, and set some property on the view to signify that the initial focus happened. In the script, you would check the flag and only focus if it hasn't been focused yet.

Without the flag, the component would re-focus every time a user would put in a new value and click off or click enter, which I assume is not what you want...

---

<div class="post-metadata">

**Author:** ![bwirth](https://avatars.discourse-cdn.com/v4/letter/b/41988e/32.png) [@bwirth](https://forum.inductiveautomation.com/u/bwirth)\
**Post date:** [January 5, 2024, 11:31pm UTC](https://forum.inductiveautomation.com/t/perspective-focus-with-selected-text/83853/7 "2024-01-05T23:31:39Z")

</div>

Probably not a bad idea. What I came up with is to loop a couple times waiting for the value to populate (with a limit. Guessing I could just for loop 5 times and break when found...)

Then set my value and wait a little more, then focus. Seemed hit or miss if I focused too fast after setting the initial value...

```auto
def runAction(self):
	import time
	
	loopcnt=0
	while self.view.custom.tagControl.currentValue is None :
		loopcnt += 1
		if loopcnt > 5 : break
		time.sleep(.1)
		
	try :
		self.props.value = self.view.custom.tagControl.currentValue
	except :
		self.props.value = 0
		
	time.sleep(.2)
	self.focus()

```

---

<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:** [January 5, 2024, 11:56pm UTC](https://forum.inductiveautomation.com/t/perspective-focus-with-selected-text/83853/8 "2024-01-05T23:56:33Z")

</div>

> [@bwirth](#):
>
> they would like is that it is focused with the number selected as when you click on a number with the mouse (as shown).

See [Perspective TextField SelectAll OnFocus - #3 by francois.morin](https://forum.inductiveautomation.com/t/perspective-textfield-selectall-onfocus/45826/3). It requires JavaScript to do it and it has not been implemented at this stage.

(I though using `sleep` was a no-no. `sleep` **and** a `while` loop! Oh-oh!)
