Delphi XE and Trapping Arrow Key with OnKeyDown

后端 未结 5 1564
闹比i
闹比i 2020-12-06 17:21

I want my form to handle the arrow keys, and I can do it -- as long as there is no button on the form. Why is this?

5条回答
  •  猫巷女王i
    2020-12-06 18:03

    Only the object that has the focus can receive a keyboard event.

    To let the form have access to the arrow keys event, declare a MsgHandler in the public part of the form. In the form create constructor, assign the Application.OnMessage to this MsgHandler.

    The code below intercepts the arrow keys only if they are coming from a TButton descendant. More controls can be added as needed.

    procedure TForm1.FormCreate(Sender: TObject);
    begin
      Application.OnMessage := Self.MsgHandler;
    end;
    
    procedure TForm1.MsgHandler(var Msg: TMsg; var Handled: Boolean);
    var
      ActiveControl: TWinControl;
      key : word;
    begin
      if (Msg.message = WM_KEYDOWN) then
        begin
          ActiveControl := Screen.ActiveControl;
          // if the active control inherits from TButton, intercept the key.
          // add other controls as fit your needs 
          if not ActiveControl.InheritsFrom(TButton)
            then Exit;
    
          key := Msg.wParam;
          Handled := true;
          case Key of // intercept the wanted keys
            VK_DOWN : ; // doStuff
            VK_UP : ; // doStuff
            VK_LEFT : ; // doStuff
            VK_RIGHT : ; // doStuff
            else Handled := false;
          end;
       end;
    end;
    

提交回复
热议问题