Board index » delphi » arrow keys and the KeyDown event

arrow keys and the KeyDown event

Hi,

I want to use the arrow keys in an application but am having difficulties. If
I set the form's keypreview to true and test for VK_LEFT etc in the KeyDown
handler nothing happens. The KeyDown event handler doesn't seem to be
triggered by the arrow keys, which seem to act only to move the focus around
the form controls. The KeyUp handler does respond to the arrow keys however.
What I would like to be able to do is to use KeyDown and not have the focus
change. Less ideal would be to use KeyUp without change of focus but I don't
know how to stop the focus moving.

This doesn't detect left arrow :

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
   if Key = VK_LEFT then
      ShowMessage('Left Arrow Down');
end;

This does :

procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
   if Key = VK_LEFT then
      ShowMessage('Left Arrow Up');
end;

Thanks in advance for any suggestions!

Regards,
Malcolm

 

Re:arrow keys and the KeyDown event


Malcolm,

Your test program is deceiving you - I'm not quite sure why. Probably the
MessageBox is hijacking the keysroke. If you monitor the state by changing a
label you will see it works OK. To stop the key having any further effect
after you have handled it, you can set it to zero.

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
   if Key = VK_LEFT then
   begin
      label1.Caption := 'Down';
      Key := 0;
   end;
end;

procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
   if Key = VK_LEFT then
   begin
      label1.Caption := 'Up';
      Key := 0;
   end;
end;

Regards,

Chris Burrows
CFB Software
http://www.cfbsoftware.com.au

Quote
"Malcolm Loudon" <M.R.Lou...@massey.ac.nz> wrote in message

news:3ad4c7ea$1@clear.net.nz...
Quote
> Hi,

> I want to use the arrow keys in an application but am having difficulties.
If
> I set the form's keypreview to true and test for VK_LEFT etc in the
KeyDown
> handler nothing happens. The KeyDown event handler doesn't seem to be
> triggered by the arrow keys, which seem to act only to move the focus
around
> the form controls. The KeyUp handler does respond to the arrow keys
however.
> What I would like to be able to do is to use KeyDown and not have the
focus
> change. Less ideal would be to use KeyUp without change of focus but I
don't
> know how to stop the focus moving.

> This doesn't detect left arrow :

> procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
>   Shift: TShiftState);
> begin
>    if Key = VK_LEFT then
>       ShowMessage('Left Arrow Down');
> end;

> This does :

> procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word;
>   Shift: TShiftState);
> begin
>    if Key = VK_LEFT then
>       ShowMessage('Left Arrow Up');
> end;

> Thanks in advance for any suggestions!

> Regards,
> Malcolm

Other Threads