Listbox is grabbing focus - How to stop

This Mac Desktop and current versions.
I wanted a small project to test focus. 2 controls on the window. Listbox and BevelButton.

I put in the gotfucus a line of code
ObjFocused = me

ObjFocused is a property

I also set a breakpoint at the line
I start up the debugger and it is always firing.
It fires on open.

The button has only one line of code in the action event.
ObjFocused = me

When I hit the button, it fires that line, but the Listbox immediately grabs the focus.

How can I stop this from happening.

You can use the control.setfocus() method to take focus away from the listbox to another control or window.

In the event for example you could use bevelButton.settocus()

(Bevel button name would be your actual control name not my example name above)


Thanks, but it didn’t work. the button got the focus and it immediately went to the Listbox. I’m going to try restarting my Mac and see if that is the problem.

By default, buttons on Mac don’t retain focus. You have to set accessibility options for the entire system to change it.

1 Like

Aah. They don’t retain focus. That means if I put TextArea then the Listbox may not grab focus.

What are accessibility options?
A search shows nothing relevant.

Tim Hare means System Preferences -> Accessibility. However, the options in there are so many that I wasn’t able to find something for buttons at first glance.

This is not a button, so it can get the Focus…

Other question is, “Is the ListBox supposed to grab focus?”
My thought is no control should be grabbing the focus.

But that’s what the Inspector focus control is made for. If you switch off the Tab Stop, the listbox will not get “first responder” status anymore.
It will also not participate in TAB-switching the focus. In case that is necessary, you will have to program that functionality yourself.

I am also trying to create a project that proves something related to focus. Focus didn’t happen when I expected it to happen.

All I am asking is when the control is being created, before the open event fires, and there is nothing which would cause an event to fire (everything except for GoTFocus) does that still mean GotFocus will fire during the creation?

There is apparently no definition of Focus.

Depending on tab stop settings, yes. While you don’t like the idea of a default focus control, the OS engineers decided so. An opening window will always try to set focus to what Apple calls “first responder” – the first control in tab order line.
You can influence this more deeply when you declare into the controls – Apple’s NSResponder class has some methods that theoretically could be overridden as events to give the controls full control over their focus. As far as Xojo is meant, tab stop and/or focus handling by code are the only means.

Thanks for confirming. GotFocus firing is also true on Windows.