Intercept TAB key in KeyPress event of a CLX TEdit

2.7k views Asked by At

I have a TEdit (VisualCLX Component) and I want to override the behaviour of the TAB Key in the onKeyPress event, but if I try to intercept the TAB key in the KeyPress event it doesn't work because the event is not fired when the tab key is pressed on the TEdit.

How can I intercept the TAB Key on KeyPress event of TEdit Control ?

1

There are 1 answers

2
moskito-x On

Edit Events does not apply to navigation keys Tab as these do not trigger keyboard events. The only place which points to this fact. Delphi Help TCustomForm.KeyPreview

To test put 3 TEdit (CLX) on a form And a TMemo.
The tab order is Edit1, Edit2, Edit3
The only event you can use is the KeyUp Event

procedure TForm1.Edit1KeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
    Memo1.Lines.Add('Edit1KeyUp');
end;

procedure TForm1.Edit2KeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
    Memo1.Lines.Add('Edit2KeyUp');
end;

procedure TForm1.Edit3KeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
    Memo1.Lines.Add('Edit3KeyUp');
    if Key = VK_TAB then begin
       Edit2.SetFocus;
    end;
end;
  • put the cursor in Edit2 input field
  • press shift tab

    • The cursor moves to the field edit1
    • Memo1 shows

      Edit1KeyUp
      Edit1KeyUp

  • press tab

    • The cursor moves to the field edit2
    • Memo1 shows

      Edit2KeyUp

  • press tab

    • The cursor tries to move to the field edit3
    • with the command Edit2.SetFocus;
      we send the cursor back to edit2
    • Memo1 shows

      Edit3KeyUp

So with the TAB key the user never can leave edit2

enter image description here

for example : In the Edit3KeyUp event you can add a #9 to to Edit2.Text.

    if Key = VK_TAB then begin
       Edit2.SetFocus;
       Edit2.Text := Edit2.Text+#9+'<- a Tab here'; 
    end;

enter image description here