Mega Code Archive

 
Categories / Delphi / VCL
 

Detecting tab key press

Question: Since the KeyPress and the KeyDown events do not get called for the tab key, how do I trap the tab key at the form level? Answer: At form level, the tab key is generally handled by Windows. The following example demonstrates creating a CM_Dialog message handler to trap for Dialog keys. The code surfaces the tab character through the KeyPress event. Example: type TForm1 = class(TForm) private procedure CMDialogKey( Var msg: TCMDialogKey ); message CM_DIALOGKEY; end; var Form1: TForm1; implementation {$R *.DFM} procedure TForm1.CMDialogKey(var msg: TCMDialogKey); begin if msg.Charcode <> VK_TAB then inherited; end; procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin if Key = VK_TAB then Form1.Caption := 'Tab Key Down!'; end;