Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2017-05

intercepting keyboard events

9 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Michael Van Canneyt

Hi,

How can I respond to keyboard events in a TCustomControl (and hence TWinControl) descendent ?

I have asigned the various OnKey* events handlers but they are not called.

My control also does not seem to receive focus when clicked, I assume this is why it doesn't get keyboard events either ?

Is there something that must be called/set when creating the control to make it receive focus ?

Michael.

Reply by email

Ondrej Pokorny
to Michael Van Canneyt
Michael Van Canneyt wrote
Is there something that must be called/set when creating the control to make it receive focus ?

If you mean receiving focus by clicking on it then yes, you need to call SetFocus in MouseDown. It's also a good idea to check the csNoFocus flag (from ControlStyle property).

For an example look how it is done in TCustomDBGrid.MouseDown.

+ The control can always get focused by the Tab key, if TabStop=True.

Ondrej

Reply by email

zeljko
to Michael Van Canneyt: How can I respond to keyboard events in a TCustomControl (and hence TWinControl)…
Michael Van Canneyt wrote 11 lines
Hi, How can I respond to keyboard events in a TCustomControl (and hence TWinControl) descendent ? I have asigned the various OnKey* events handlers but they are not called. My control also does not seem to receive focus when clicked, I assume this is why it doesn't get keyboard events either ?

Yep

Is there something that must be called/set when creating the control to make it receive focus ?

Just to be sure that control actually can take focus put this ControlStyle := ControlStyle - [csNoFocus];
into constructor of your control.

z.

Reply by email

Graeme Geldenhuys
to zeljko
zeljko wrote
Just to be sure that control actually can take focus put this ControlStyle := ControlStyle - [csNoFocus]; into constructor of your control.

And what if that doesn't work? I've set that in the constructor, but still it doesn't receive keyboard focus (after I've clicked on the custom control)?

What else can I try?

eg:
I have a PageControl which contains my custom control. I then have a treeview on the right. If the treeview had focus, and I click on my custom control and then press some keys (eg: Up/Down) then the focused node in the treeview changes. Keyboard focus doesn't seem to want to move to the custom control.

ps #2:
Interacting with a mouse on my custom control seems to work (but then I have SetFocus in the MouseDown too).

Regards,
Graeme

Reply by email

Ondrej Pokorny
to Graeme Geldenhuys
Graeme Geldenhuys wrote 21 lines
On 2017-05-08 17:46, zeljko via Lazarus wrote: > Just to be sure that control actually can take focus put this > ControlStyle := ControlStyle - [csNoFocus]; > into constructor of your control. And what if that doesn't work? I've set that in the constructor, but still it doesn't receive keyboard focus (after I've clicked on the custom control)? What else can I try? eg: I have a PageControl which contains my custom control. I then have a treeview on the right. If the treeview had focus, and I click on my custom control and then press some keys (eg: Up/Down) then the focused node in the treeview changes. Keyboard focus doesn't seem to want to move to the custom control. ps #2: Interacting with a mouse on my custom control seems to work (but then I have SetFocus in the MouseDown too).

Zeljko was wrong in his statement. The csNoFocus is just a helper flag that custom controls should follow but don't have to. TWinControl directly doesn't do anything. with csNoFocus - it doesn't have any meaning for it.

Follow my instruction from my previous email in this thread:

If you mean receiving focus by clicking on it then yes, you need to call SetFocus in MouseDown. It's also a good idea to check the csNoFocus flag (from ControlStyle property).

For an example look how it is done in TCustomDBGrid.MouseDown.

+ The control can always get focused by the Tab key, if TabStop=True.

Ondrej

Reply by email

Ondrej Pokorny
to Graeme Geldenhuys
Graeme Geldenhuys wrote 8 lines
On 2017-05-22 15:01, Ondrej Pokorny via Lazarus wrote: > For an example look how it is done in TCustomDBGrid.MouseDown. > > + The control can always get focused by the Tab key, if TabStop=True. I'll double check that as well, just to be sure. Thanks to both for the very prompt replies. Much appreciated.

I checked again and it seems that Zeljko wrote Qt bindings so that they take care of the csNoFocus flag. But no other WidgetSet does so. So there probably is an incompatibility between LCL WidgetSets regarding this.

csNoFocus has no effect on plain win32 TWinControl and it's descendants that do not handle csNoFocus explicitely (and are not bound to native widgets - they are special, see below).

It is also so that some (maybe all) native win32 widgets (edit, button etc.) always get focused automatically if they are clicked on with the mouse and they are enabled - you cannot have a clickable TButton that doesn't receive focus when clicked on but receives focus with TAB key. With a custom non-native button it is perfectly possible.

Ondrej

Reply by email