Perspective focus with selected text

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

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

Thank you, that is the way I have it.

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?

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

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).

1 Like

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...

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...

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()

See Perspective TextField SelectAll OnFocus - #3 by francois.morin. 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!)

2 Likes