[Lazarus] intercepting keyboard events

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[Lazarus] intercepting keyboard events

Free Pascal - Lazarus mailing list

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.
--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus-ide.org/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Lazarus] intercepting keyboard events

Free Pascal - Lazarus mailing list
On 08.05.2017 17:42, Michael Van Canneyt via Lazarus 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


--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus-ide.org/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Lazarus] intercepting keyboard events

Free Pascal - Lazarus mailing list
In reply to this post by Free Pascal - Lazarus mailing list
On 08.05.2017 17:42, Michael Van Canneyt via Lazarus wrote:

>
> 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.

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus-ide.org/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Lazarus] intercepting keyboard events

Free Pascal - Lazarus mailing list
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).

Regards,
   Graeme

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus-ide.org/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Lazarus] intercepting keyboard events

Free Pascal - Lazarus mailing list
On 22.05.2017 15:57, Graeme Geldenhuys via Lazarus wrote:

> 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
--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus-ide.org/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Lazarus] intercepting keyboard events

Free Pascal - Lazarus mailing list
In reply to this post by Free Pascal - Lazarus mailing list
On 22.05.2017 16:57, Graeme Geldenhuys via Lazarus wrote:
> What else can I try?

Adding public SetFocus; override;--

procedure TATSynEdit.SetFocus;
begin
   LCLIntf.SetFocus(Handle);
end;

--

Regards,
Alexey

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus-ide.org/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Lazarus] intercepting keyboard events

Free Pascal - Lazarus mailing list
In reply to this post by Free Pascal - Lazarus mailing list
On 2017-05-22 14:57, Graeme Geldenhuys via Lazarus wrote:
> And what if that doesn't work?

Ignore that. I forced a Rebuild of all units, and now suddenly it is
working as expected.

Regards,
   Graeme

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus-ide.org/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Lazarus] intercepting keyboard events

Free Pascal - Lazarus mailing list
In reply to this post by Free Pascal - Lazarus mailing list
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.


Regards,
   Graeme

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus-ide.org/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Lazarus] intercepting keyboard events

Free Pascal - Lazarus mailing list
On 22.05.2017 16:05, Graeme Geldenhuys via Lazarus wrote:
> 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
--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus-ide.org/listinfo/lazarus
Loading...