Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Key processing in controls

25 views
Skip to first unread message

Kees Vermeulen (Kever-IT)

unread,
Jul 7, 2008, 3:42:31 AM7/7/08
to

Hello,

I have a problem with Key processing in controls when they are located
on a modal form. I want all character keys to be handled by this
control, however, when the key matches the hotkey of another control on
the form (like the 'C' of a close button) the action associated with
this control is executed before my control can handle the key.

I have been experimenting with handling event CM_CHILDKEY which enabled
me to trap all keys but unfortunately also stopped normal key processing
for the control (events KeyPress and KeyUp are no longer called).

Any idea how to resolve this?

Regards,

Kees Vermeulen
Kever-IT

This is the sample control:

TKeyPanel = class(TPanel)
protected
procedure KeyDown(var Key: Word; Shift: TShiftState); override;
procedure KeyPress(var Key: Char); override;
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('Test', [TKeyPanel]);
end;

{ TKeyPanel }

procedure TKeyPanel.KeyDown(var Key: Word; Shift: TShiftState);
begin
Caption := Caption + Char(Key);
end;

procedure TKeyPanel.KeyPress(var Key: Char);
begin
Caption := Caption + Key;
end;

Peter Below (TeamB)

unread,
Jul 7, 2008, 1:45:31 PM7/7/08
to
Kees Vermeulen (Kever-IT) wrote:

What so you think will happen here if the key pressed is one that does
not produce a character? If you want characters use *only* KeyPress, if
you need other keys as well (arrow keys, for example) use KeyDown for
them (and *only* for them).

> procedure TKeyPanel.KeyPress(var Key: Char);
> begin
> Caption := Caption + Key;
> end;

If your control has the focus then all you need is to tell the VCL that
you want to get key messages. You do that by handling the WM_GETDLGCODE
message. A panel is also not focusable by default (it will not grab the
focus when you click on it). Here is an example of what you need to do
(i used an interposer class for testing, just change the class name to
TKeyPanel):

Tpanel = class(ExtCtrls.TPanel)
protected
procedure WMGetDlgCode(var Message: TWMGetDlgCode);
message WM_GETDLGCODE;


procedure KeyDown(var Key: Word; Shift: TShiftState); override;
procedure KeyPress(var Key: Char); override;

procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
X: Integer; Y: Integer); override;
public
function CanFocus: Boolean; override;
end;

function Tpanel.CanFocus: Boolean;
begin
result := true;
end;

procedure Tpanel.KeyDown(var Key: Word; Shift: TShiftState);
begin
if key = VK_DELETE then
Caption := ''
else
inherited;
end;

procedure Tpanel.KeyPress(var Key: Char);
begin
if Key >= ' ' then


Caption := Caption + Key

else if (Key = #8) and (Length(Caption) > 0) then
Caption := Copy(Caption, 1, Length(Caption)-1)
else
inherited;
end;

procedure Tpanel.MouseDown(Button: TMouseButton; Shift: TShiftState; X,
Y: Integer);
begin
inherited;
if not Focused then
SetFocus;
end;

procedure Tpanel.WMGetDlgCode(var Message: TWMGetDlgCode);
begin
Message.Result := DLGC_WANTCHARS;
end;

There *are* edit controls, by the way <g>.

--
Peter Below (TeamB)
Don't be a vampire (http://slash7.com/pages/vampires),
use the newsgroup archives :
http://www.tamaracka.com/search.htm
http://groups.google.com

Kees Vermeulen (Kever-IT)

unread,
Jul 8, 2008, 3:03:31 AM7/8/08
to

Peter,

Thanks for your extensive answer, it clarified things a lot for me.

> There *are* edit controls, by the way <g>.

I might even want to use them some day.

Regards,

Kees Vermeulen
Kever-IT

Peter Below (TeamB) schreef:

0 new messages