Quick tip – assigning Command+Letter shortcuts to FMX menu items

It’s not obvious since the forms designer doesn’t support it, but the way to assign a Cmd+Letter shortcut to a menu item in FireMonkey/OS X application in XE2 is to do so at runtime. For example, say you have a ‘file open’ item called itmFileOpen, and want to assign it the shortcut Cmd+O; to do so, create an OnCreate event handler for the form by double clicking it, then add the following line:

itmFileOpen.Shortcut := scCommand or Ord('O');

At runtime, this will ensure the proper shortcut symbol appears next to the item (⌘O), as well as make the actual key combination work.


11 thoughts on “Quick tip – assigning Command+Letter shortcuts to FMX menu items

  1. This works, but I’ve observed that it overrides the “modal-ness” of any window that would grays-out the menu. My guess is that it should not work that way, but maybe that’s the way OSX generally works?

      • Sure. I followed the hint you gace, which was very helpful. So now my user calls up a window that is modal, so the main menu grays out and cannot be clicked, However, despite the main menu not being “clickable”, the shortcut key still works.


      • “Follow?”

        Nope… I’ve tried this:

        1. Start a new FMX HD application.
        2. Add a TMainMenu to the form, and added a File top level item and a Test item below that.
        3. Added a new form to the project.
        4. Back in Unit1.pas, added Unit2 to the uses clause, then handled the test item’s OnClick event like this:

        procedure TForm1.itmTestClick(Sender: TObject);
          with TForm2.Create(nil) do

        5. Handled the form’s OnCreate event to assign the shortcut key:

        procedure TForm1.FormCreate(Sender: TObject);
          itmTest.ShortCut := scCommand or Ord('T');

        6. Run the application, and press Cmd+T; the second form shows. Press Cmd+T again; there’s just a ‘beep’.

        I’m using XE2 update 4, hotfix 1.

        • OK, now add this to your TForm1.Create Event:

          Quit1.ShortCut := scCommand or Ord(‘Q’);

          where Quit1 is the name of the Quit item on the main menu. While TForm2 is visible, you should not be able to access the menu on TForm1, but command-Q will, at least in my experience, quit the project.

        • Playing around, I’ve found the Cmd+Q shortcut being effective completely independent of what I do – i.e., I don’t even need to add the line you suggest, since Cmd+Q is a global shortcut for terminating the running application. Try commenting the line out – do you get the same?

          • You are indeed correct. The only thing my code accomplishes is to display the apple-Q in the menu next to the QUIT item. It’s the only one I wanted a shortcut for and it never occurred to me to try it without showing it attached to some item in the menu. Thanks.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s