whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1541)

Database (90)
Files (137)
Forms (107)
Graphic (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Math (76)
Misc (126)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (266)
VCL (242)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

38 Visitors Online


 
...capture the Tab Key in a TEdit?
Autor: Thomas Stutz
[ Print tip ]  

Tip Rating (40):  
     


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;


 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners