OnKeyPress Event

I'm using the event "OnKeyPress" and with a condition I want to know if the user is pressing on the TAB key  (ASCII #09).  I know if the user is pressing on the "o" key or the "1" key (etc), but it doesn't work with the TAB key.  How to know that?  Could be good if using the OnKeyDown envent also.

Thanks,
Elbaid.
elbaidAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

intheCommented:
Arrow keys and tabs do not generate WM_KEYDOWN so the The KEYDOWN event does not get fired for the left/right arrow keys or tab.
You need to handle WM_GETDLGCODE to tell Windows what keyboard messages you want to process.

 
private
 procedure WMGetDlgCode(var message: TMessage); message WM_GETDLGCODE;
   
  end;

// request the arrow keys + characters and tabs
procedure TForm1.WMGetDlgCode(var message: TMessage);
begin
  message.Result := DLGC_WANTARROWS or DLGC_WANTCHARS or DLGC_WANTTAB;
end;
 

now you should get the tab key..
Regards Barry

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
k6__Commented:
I Think this will do your job =)

unit Unit1;

interface

uses
  Windows, Classes, Forms, Dialogs;

type
  TForm1 = class(TForm)
    procedure FormKeyUp(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
 Case Key Of
  13   : ShowMessage('Enter');
  9    : ShowMessage('Tab');
  27   : ShowMessage('Esc');
  Else   ShowMessage(Char(Key));
 End;
end;

end.
0
IndefreiCommented:
work on keydown only
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
{ Shift   = 16        F1   = 112     Strg + 0  = 48
  Strg    = 17        F2   = 113     Strg + 1  = 49
  Alt     = 18         .      .          .        .
  Pfeil u = 40         .      .          .        .
  Pfeil r = 39         .      .          .        .
  Pfeil o = 38      - F12  = 123     Strg + 9  = 57
  Pfeil l = 37
  Ende    = 35      NUM u  = 144     Strg + TAB = 9
  Pos1    = 36      Rollen = 145     Shift feststellung = 20
  Bild o  = 33      Pause  =  49
  Bild u  = 34
  Entf    = 46
  Einf    = 45

}


//   Label2.caption:=inttostr(key);

   case key of
   9 :
..
..
..
Indi
0
IndefreiCommented:
Hei, you are so quick
and
the arrows (Pfeil) work in my app , so what ....??
Indi
0
intheCommented:
yep done a test the following will work:


unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    procedure FormKeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }
    procedure WMGetDlgCode(var message: TMessage); message WM_GETDLGCODE;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.WMGetDlgCode(var message: TMessage);
begin
  message.Result := DLGC_WANTTAB;
end;




procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
begin
setfocus;
if (key = #9) then showmessage('tab pressed')
else key := #0;
end;

end.


but for an edit control (or some other control that has focus) use the following instead:

procedure TForm1.Edit1KeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
if (key = VK_TAB) then showmessage('tab pressed');
end;

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.