Home > Net >  How do I make Enter key behave as a tab in a Delphi FireMonkey Application?
How do I make Enter key behave as a tab in a Delphi FireMonkey Application?

Time:03-15

Previously in Delphi VCL applications it was easy to "over ride" the key strokes on either the onkeyup or onkeydown events of components to make the Enter key behave as a TAB key. FireMonkey applications work differently from VCL, so how should one do this now?

CodePudding user response:

The following code is a procedure which can be used from a global library or TDataModule to provide you with the Enter to Tab functionality. I used the onkeyup events on inputs to test it.

procedure HandleEnterAsTab(Form: TForm; Sender: TObject; Key: Word);
var
  TabList: ITabList;
  CurrentControl, NextControl: IControl;
begin
  if (Key = vkReturn) then
  begin
    TabList := Form.GetTabList;
    CurrentControl := TControl(Sender);
    NextControl := TabList.FindNextTabStop(CurrentControl, True, False);
    if (NextControl = nil) then  //go to first item if reached end
    begin
      NextControl := TabList.GetItem(0);
    end;
    
    NextControl.SetFocus;
  end;
end;

The following is an example snippet of it's use

procedure TForm1.Edit2KeyUp(Sender: TObject; var Key: Word; var KeyChar: Char;
  Shift: TShiftState);
begin
  HandleEnterAsTab(Form1, Sender, Key);
end;

Obviously you could change the procedure to work differently according to your needs however I have tried to make it as generic as possible by using TComponent and TForm as the container for getting the TabList.

CodePudding user response:

As I already mentioned in a comment, another way is to drop a TButton on the form, set TabStop = False and Default = True. The make it small and hide it under another control.

In the OnClick event of the button execute the following code:

var
  ch: Char;
  key: Word;
begin
  key := vkTab;
  ch := #9;
  KeyDown(key, ch, []);
end;

Note that any other button having focus takes precedence over this, so the expected behavior is preserved.

  • Related