Window background get focus if clicked

Hi,

I made a Canvas class and I have a few instances on a window. With mouseclick I can activate them (gotfocus/lostfocus). If click out of them the last clicked is still in focus.
What I would like is if I click on an empty area on the window that deactivate all of them (focus on the background or something).
My first idea was to put full window size canvas in the background but even if I set it to transparent it covers everything.

Thanks,
Thomas

You do not read the Window Class in the LR.

Go there and search for Focus.

In code, Window1.MouseDown (or Window1.MouseUp) Event, set the Focus to Nil; that will clear the Focus from the Control that had it.

You will understand while reading the documentation.

Well, I tried Window1.MouseDown but it activates even if I click on the canvas so overwrites everything or I have to check the mouse coordinates if they are on any UI element…

You have to change the Window Focus there…

So, in Window1.MouseDown you will add:

Window1.Focus = Nil

AFAIK

In case of trouble, set a Break into Window1.MouseDown top of the event (before any other line) and look the .Focus value.

1 Like

Dont’t forget:

ClearFocus ()
1 Like

Again, the problem is Window1.MouseDown fires wherever I click on the window (canvas or background) so I can’t use it.
Also, it would be nice to handle this in my class if possible

Make sure you are returning True in MouseDown on your canvas so the event won’t propagate.

2 Likes

This is where the chapter on the Event Model in my JavaScript book was so handy. It told me what events propagated, in which direction, and how to prevent it.

This lack of a comprehensive description of the Xojo event model has, IMO, bedevilled understanding.

1 Like

Did you mean something like the example?
simple controls

https://www.dropbox.com/scl/fi/iyt8u8rc0j044kf61bdrq/fokustest.xojo_binary_project?rlkey=9k91vthi4c817mnzmsfmxd09k&dl=1

That’s it, Return True in Canvas MouseDown and Window1.Focus = Nil in window MouseDown

Thanks

P.S. It’s working but I not understand what ‘Return True’ does…

Greg had this answer for you:

It is also explained in the documentation: Canvas.MouseDown

1 Like

Thanks

Trouble is (again) that the doc doesn’t mention explicitly that if you return False the event gets propagated upwards.