Search code examples
formsdelphihotkeys

How to I keep forms from handling hotkeys when they don't have the focus?


We have a Delphi XE2 application. Form1 has hotkey Ctrl+F, and Form2, which is active, without hotkeys. The user presses Ctrl+F on Form2 and Form1 processes the hotkey. It's totally incorrect because we see activity of the inactive form. How do I fix it?


Solution

  • I implement all short cut key handling using actions. If you do this then you can use the centralisation that actions, action lists, action managers etc. provide to enable and disable all actions based on whether or not a form is active.

    Do that, for example, by setting the action list's State property on the OnActivate and OnDeactivate event handlers of the form:

    procedure TMyForm.FormActivate(Sender: TObject);
    begin
      ActionList.State := asNormal;
    end;
    
    procedure TMyForm.FormDeactivate(Sender: TObject);
    begin
      ActionList.State := asSuspended;
    end;