Determining if a mobile control has the focus

Feedback Case Number: https://tracker.xojo.com/xojoinc/xojo/-/issues/74493

Is there a way for the MobileCanvas to know if it has received or lost the focus? As far as I can see there is no event for this.

I find this odd given that there is a SetFocus() method in the iOS framework but I’m not quite sure if it’s working properly.

This is making porting my code editor to iOS difficult.

If the keyboard appears then the control is getting focus.

Yeah but how do I detect that in a mobile canvas?

In the desktop version of my editor, the caret stops blinking when the canvas no longer has the focus. I set the the focus when the canvas is clicked on.

The only events I can see that are relevant are KeyDown and TextReceived. However relying on those means the caret won’t get activated until after a key is pressed…

Would a Boolean property telling you that it has focus help?

Yes it would.

Internally within my control I have a Timer that fires (to draw the caret) that checks a boolean property to see if the canvas has the focus. On desktop I set this in the FocusReceived event but there isn’t an equivalent for iOS.

ok, so after doing a little hacking, The common wisdom (even in Obj-C) seems to be to put an invisible text control on the view and use the events from that control.

I was able to hack a MobileContainer to respond correctly to a request to “becomeFirstResponder” but the underlying control itself never really did and doesn’t seem to fire the events anyway.

Thanks for taking a look at this but crikey, what an ugly workaround.

There are two scenarios where I would want the caret/insertion point to be blinking in a code editor:

  1. When the on-screen keyboard is displayed. As far as I can see, there is no event in the iOS framework that fires when the keyboard is presented or dismissed. Do you know of one?

  2. If an external keyboard is attached to the iPad. In this scenario I’d want to know one was present and always blink the caret.

Right, so the problem is that the keyboard has to be shown manually, so there is no “event” to attach to and as you state here:

The on-screen keyboard does not appear if an external keyboard is attached.

It may be, but it’s what people are using. So put your canvas and a hidden texfield in a container and capture touches to set focus on the field.

2 Likes