Keydown in canvas


I’m trying to get the keydown event working with a canvas in RS2012R2.1
I put this in the keydown event:


But, I’m pressing like a mad man, nothing happens, what did I forgot?


Canvas cannot get the focus. You may want to use the window keydown instead.

Canvases can get focus. You have to set “AcceptFocus” to True (either in the IDE or in code). Then it will work.

I had not noticed. It works indeed. Thanks.

I noticed that in Cocoa, though, if a TextField is present, it takes the focus, in spite of a Canvas.Setfocus.

Just tested “Canvas1.SetFocus” it in the window’s open event in Cocoa, and no problem, Canvas1 receives the focus ring. Where is your Canvas1.SetFocus placed?

In the window open event. and it works fine. But if I add a textfield with TabIndex 0, it snatches the focus…

That’s correct. TabIndex 0 means it’s the control getting the focus when opening the window.

Sure. But when I set Canvas with TabIndex 0, it still goes to the first text control in the tabindex order…

You probably have to controls with a TabIndex of 0. Don’t set TabIndex. Use the “Tab order editor” - then you can be sure, that you don’t use the same index twice.

Amazingly enough, Canvas has a tabindex or zero, and I checked in the tab order editor. When the window opens, focus is on a textarea tabindex 1. When I cycle through, after the focus is on tabindex 4, it reverts correctly to Canvas TI 0. For some reason, it’s at the window opening that focus goes to control TI 1. I was doing that to help out in the Tab problem of Seymour in another thread, so I am not stuck on it, but it sure does not react as expected.

As I wrote in my previous post, I tested it quickly and it works properly focusing the Canvas1 if Canvas1 has a TabIndex of 0. So it must be something different.

In general:

On Cocoa the tab oder is determined by the system, see Tab Order Change in Real Studio 2012r1.

But be aware, that this is not from left to right and then from top to bottom, AFAIK the order is the same the order in which the controls are added to the window. This means, the control instance created first, will be “addSubview”-ed first within the Xojo framework, and so on.