Search code examples
delphifiremonkeyshiftonkeydown

How to assign shortcuts to a firemonkey TForm OnKeyDown Event:


I'm having trouble assigning a shortcut to a TabItem on Firemonkey, using the form's OnKeyDown event.

It seems that the ctrl key works like it's being pressed and released over and over again.

Here is what I'm trying to do:

procedure TfrmPrincipal.FormKeyDown(Sender: TObject; var Key: Word;
  var KeyChar: Char; Shift: TShiftState);
begin
   if ssCtrl in Shift then
   begin
      if UpperCase(KeyChar) = 'L' then
       TabItem1.SetFocus;
   end;
end;

It doesn't work and after googling a while and found a lot of VCL driven answers, I couldn't find anything that can help me with Firemonkey.

I'm using Tokyo 10.2.2

Any tips?

Thanks in advance.


Solution

  • You better use an actionlist with an action for that:

    procedure TTabbedForm.Action1Execute(Sender: TObject);
    begin
      TabControl1.ActiveTab := TabItem1;
    end;
    
    object ActionList1: TActionList
      Left = 176
      Top = 272
      object Action1: TAction
        Text = 'Action1'
        ShortCut = 16460
        OnExecute = Action1Execute
      end
    end
    

    About your code:

    TabItem1.SetFocus is not working, use TabControl1.ActiveTab := TabItem1;

    KeyChar in FormKeyDown doesn't have a value when Ctrl key is pressed, use Key for that.