Board index » delphi » Why can't I catch a KeyDown event when I press the tab key in a form ?

Why can't I catch a KeyDown event when I press the tab key in a form ?

        When I use a form, I prefer to use the ENTER key to move from a field
to next one, rather than the TAB key. I wanted to use the OnKeyDown event,
trying to catch its Key parameter ... But when the tab key is pressed, it seems
that the OnKeyDown event isn't called. I then tried to write my own
TMyForm.KeyDown procedure, overriding the inherited one. Once again, it just
failed (did I say the KeyPreview property had before been set to True ?).

procedure TMyForm.KeyDown(var Key: Word; Shift: TShiftState);
begin
        if Key = VK_ENTER then Key := VK_TAB;
        inherited;
end;

If you know what's wrong with this, please let me know, it would be very
helpful. I've lost a lot of hours trying to solve this - unsuccessfully ... and
I am now completely puzzled. My e-mail is duran...@worldnet.fr

        Thanks in advance    :-)

 

Re:Why can't I catch a KeyDown event when I press the tab key in a form ?


If you want to use the enter key as a tab key you might try this in the
OnKeyPress event of your form. Be sure keypreview is set to true.

if key=#13 then
  begin
    {change focus to next control}
    SelectNext(ActiveControl as TWinControl, True, True);
    {Clear "key" to skip default handling}
    Key:=#0;
    exit;
  end;

Re:Why can't I catch a KeyDown event when I press the tab key in a form ?


Quote
gouy wrote:

>         When I use a form, I prefer to use the ENTER key to move from a field
> to next one, rather than the TAB key. I wanted to use the OnKeyDown event,
> trying to catch its Key parameter ... But when the tab key is pressed, it seems
> that the OnKeyDown event isn't called. I then tried to write my own
> TMyForm.KeyDown procedure, overriding the inherited one. Once again, it just
> failed (did I say the KeyPreview property had before been set to True ?).

> procedure TMyForm.KeyDown(var Key: Word; Shift: TShiftState);
> begin
>         if Key = VK_ENTER then Key := VK_TAB;
>         inherited;
> end;

GExpert tells me this:

{This technique may be helpful to you if you want the tab to
 move columns with in a stringgrid, etc. instead of performing
 its default action}

unit TrappingTabs;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs,
  StdCtrls, Grids;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    { Private declarations }
    procedure WMGetDlgCode(var msgIn: TWMGetDlgCode); message
WM_GETDLGCODE;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.WMGetDlgCode;
begin
   inherited;
   msgIn.Result := msgIn.Result or DLGC_WANTTAB;
end;

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key = VK_TAB then
     ShowMessage('Tab');
end;

end.

Re:Why can't I catch a KeyDown event when I press the tab key in a form ?


Gouy,

By default, key downs are invisible to the form. You have to set the
form property KeyPreview to true.
--
--------------------
Mark Bratcher
<A HREF="mailto:MarkBratc...@csi.com">MarkBratc...@csi.com</A>
--------------------

Quote
gouy wrote:

>         When I use a form, I prefer to use the ENTER key to move
from a field
> to next one, rather than the TAB key. I wanted to use the
OnKeyDown event,
> trying to catch its Key parameter ... But when the tab key is
pressed, it seems
> that the OnKeyDown event isn't called. I then tried to write my
own
> TMyForm.KeyDown procedure, overriding the inherited one. Once
again, it just
> failed (did I say the KeyPreview property had before been set to
True ?).

> procedure TMyForm.KeyDown(var Key: Word; Shift: TShiftState);
> begin
>         if Key = VK_ENTER then Key := VK_TAB;
>         inherited;
> end;

Other Threads