Search code examples
delphishortcuttactiontlistboxtmenuitem

Delphi: How to Assign an Up Arrow Keyboard Shortcut to Action/MenuItem, and Keep It Actual for Navigating the List Control (ListBox/VTV)?


Please assist me: How to assign an up arrow keyboard shortcut to action or menu item, and keep it actual for navigating the list control (e.g. ListBox/Virtual Treeview/other) at the same time?

Thanks!


Solution

  • You comment:

    And how about the Winamp player? It has Volume Up/Volume Down features assigned to the up arrow key and down arrow key correspondingly.. Okay, if that impossible in Delphi, then ...

    but it certainly is possible, it just isn't a good idea to do it, and against the Windows User Experience Interaction Guidelines.

    But if you're set on implementing it, here's how. Override the following method in your form class that contains the action components:

    function IsShortCut(var Message: TWMKey): Boolean; override;
    

    and in it you can prevent the Up and Down key from triggering the actions they are shortcuts for:

    function TWeirdForm.IsShortCut(var Message: TWMKey): Boolean;
    begin
      if (Message.CharCode in [VK_UP, VK_DOWN])
        // insert test whether message needs to go to the focused control instead
        and (...)
      then begin
        // insert calls to code that should be executed instead
        Result := False;
        exit;
      end;
      inherited;
    end;
    

    Note that you should test for the correct shift state too, and check that your code doesn't break any other window behaviour users expect, like moving of the window with the arrow keys.