Auto Select Text Box on Start

Hi all,

As the title says, I’m trying to make it so that when a project view is opened the user can start typing directly into a text box without having to click on it first. I’ve been looking around and I haven’t been able to find anything yet. Does anyone have a suggestion on how to accomplish this?

Thanks!

virtual or physical keyboard?

In the Startup Event of the component (or of the View), specify your Text Field and give it focus.

Startup Event of the Text Field:

self.focus()

Startup Event for the View:

# you'll need to drill down through the component hierarchy to locate your text field here
self.root.getChild("Text Field").focus()
2 Likes

Thank you all! I knew it was simple and that the solution was out there but I was struggling with the search on the forum. Appreciate the response!

1 Like

Sorry to open this up again, but I’m having trouble getting focus() to work. It works perfectly when I tested it in an empty view w/just a text box, but I’m trying to incorporate it on a popup that also has other components. It doesn’t want to work no matter where I make the focus call. I’m not sure whether its because there’s other components that are loading after the call and that’s messing it up somehow, or if it has to do with the fact that it’s a popup and that’s confusing it somehow. Any advice?

Are you using both of the suggestions that @cmallonee suggested? I believe the intent was that either of those events would do the job, but you probably should only be using one or the other.

No, just trying one at a time to see what works

It’s hard to say why it’s not working without taking a look at the popup view itself. Is it possible that there is another component in the popup view calling focus() as well (e.g. if the original textfield that had your onStartup event was duplicated, but the event was not removed from the duplicate textfield)?

The fact that your textfield is contained within a popup should not matter though; I am using this method on a few different popups on some projects currently and it seems to work fairly reliably.

If you could supply the View in question, I could take a look at it. The only reason I would expect this not to work is if the Popup were to open with any piece of itself outside the bounds of the viewport.

The issue at play was a race condition between the Popup requesting focus and the execution of the onStartup Event code for the View and the Text Field. The following code placed into the onStartup Event of the popup View works in my environment, albeit after a one-second “pause”.

	def tf_focus():
		from time import sleep
		sleep(1)
		self.getChild("mfaToken").focus()
	system.util.invokeAsynchronous(tf_focus)