Shortcut Keys In Delphi MDI Applications

Predefined Windows MDI Shortcut Keys - Or "Why CTRL+F4 closes my child form?"

In an MDI application, more than one document or child window can be opened within a single parent window.

Even though MDI as a concept was designed decades ago, there are still new applications being developed using MDI approach - or old applications that you need to maintain.

Yes, new Windows UI approach is more toward TDI (Tabbed User Interface) - but MDI is still here and will be here for long time.

Providing shortcut keys for some common actions in your MDI child forms is something that will make your application more user friendly. Do you know there are some shortcuts you cannot use?

Shortcut Keys in MDI

In an MDI application I am working on for the last few days, just recently I had to implement keyboard shortcuts for some of the actions (toolbar buttons) a user can do.

Simply by accident the first shortcut key I assigned to some of the actions for the user was "CTRL + F6".

I've placed the code in the OnShortCut even handler for the mdi child form. My code was NOT executed, rather the following happened: the "next" MDI child windows was activated!

By looking at the implementation of what calls the OnShortCut event I've located a call to IsMDIMsg in the TApplication.ProcessMessage implementation.

The IsMDIMsg calls the Windows API TranslateMDISysAccel that finally handles the DEFAULT MDI shortcuts!

Actually, the TranslateMDISysAccel processes accelerator keystrokes for window menu commands of the multiple-document interface (MDI) child windows associated with the specified MDI client window.

Shortcut Keys "Free" To Use in MDI Applications

Here's the list of the shortcut keys predefined by Windows, when MDI applications are in question (therefore key combinations you cannot use for your own actions)
[Alt + -] Displays the Multiple Document Interface (MDI) child window's System menu (from the MDI child window's System menu, you can restore, move, resize, minimize, maximize, or close the child window)
[Ctrl + F4] Closes the current Multiple Document Interface (MDI) window
[Ctrl + F6] Switch to the next child window of a Multiple Document Interface (MDI) program.
[Ctrl + Tab] Switch to the next child window of a Multiple Document Interface (MDI) program
[Ctrl + Shift + Tab] Switch to the previous child window of a Multiple Document Interface (MDI) program
Note: "[Alt + -]" means pressing the "-" key (hyphen) with ALT key being down.

OnShortCut

For other shortcut keys that you want to use you can handle those in the OnShortCut even of the child window:
procedure TMDIChildBaseForm.FormShortCut(var Msg: TWMKey; var Handled: Boolean);
begin
  if GetKeyState(VK_CONTROL) < 0 then // CTRL+
  begin
    case Msg.CharCode of
      Ord('E') : //do something when CTRL + E;
      VK_ESCAPE : //do something when CTRL + ESC;
      VK_F12 : //do something when CTRL + F1
    end;
  end;
end;
Note: If in the OnShortCut you implement a response to the keystroke, set the Handled parameter of the event handler to true. This prevents the keystroke from being passed on to menus or actions associated with the form. It also prevents the standard keystroke processing in the same way that a menu or action shortcut does.

More info on: virtual keys and ctrl / alt keys

That's it. Now you know what shortcuts you cannot use and how to handle the rest in your MDI applications.