elbaid
asked on
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.
Thanks,
Elbaid.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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(k ey);
case key of
9 :
..
..
..
Indi
procedure TForm1.FormKeyDown(Sender:
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(k
case key of
9 :
..
..
..
Indi
Hei, you are so quick
and
the arrows (Pfeil) work in my app , so what ....??
Indi
and
the arrows (Pfeil) work in my app , so what ....??
Indi
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;
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
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;
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.