Mega Code Archive

 
Categories / Delphi / VCL
 

How to capture the Tab Key in a TEdit

Title: How to capture the Tab Key in a TEdit Problem/Abstract: It's not possible to trap the tabulator-key in the OnkeyPress/OnkekDown/OnkeyUp handler of a TEdit control. Here are two ways to trap it! Problem: Es ist nicht m glich, im OnkeyPress/OnkekDown/OnkeyUp eines TEdits festzustellen, ob die Tabulatortaste gedr ckt wurde. Hier sind zwei M glichkeiten, wie man sie trotzdem abfangen kann!} private Procedure CMDialogKey(Var Msg: TWMKey); message CM_DIALOGKEY; {...} Procedure TForm1.CMDialogKey(Var Msg: TWMKEY); Begin If (ActiveControl = Edit1) Then If Msg.Charcode = VK_TAB Then begin ShowMessage('Tab'); { Msg.Charcode := 0; } // to eat the tab key! end; inherited; End; {**** Or/ Oder: ****} private Procedure CMDialogKey(Var Msg: TWMKey); message CM_DIALOGKEY; {...} procedure TForm1.CMDialogKey(var Msg: TWMKey); begin if (Edit1.Focused) then Msg.Result := 0 else inherited; end; procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char); begin if Ord(Key) = VK_TAB then begin ShowMessage('Tab'); Key := #0; // no beep! end; end; {**** Or/ Oder: ****} (by Peter Larson) {By adding a message handler for WM_GETDLGCODE to a component, it is possible for a component to capture the tab key. The tab key will be passed to the OnKeyDown event handler like other keys. The following D4 code shows what needs to be added to a component to allow the capture of the tab key.} Type TExtEdit = Class(tEdit) Private procedure WMGetDlgCode(var Message: TWMGetDlgCode); message WM_GETDLGCODE; End; {...} procedure TExtEdit.WMGetDlgCode(var Message: TWMGetDlgCode); Begin Inherited; Message.Result := Message.Result or DLGC_WANTTAB; End;