🚀 Welcome to MDriven Learn –  MDriven is now on Discord!  Don’t miss the latest Release Notes.
Use WPF Menu Shortcut Keys
This page was created by Wikiadmin on 2026-07-29. Last edited by Wikiadmin on 2026-07-29.

You can assign shortcut keys to actions in MDriven Designer and make a custom WPF/WECPOF host recover when WPF loses keyboard focus, so action shortcuts such as ctrl+s continue to work.

Configure a shortcut on an action

A shortcut key belongs to an individual action. Configure it on the action in MDriven Designer.

  1. Select the action.
  2. Set its Shortcut Key value.
  3. Enter the key combination in the shortcut-key field, for example ctrl+s for Save, ctrl+z for Undo, or ctrl+y for Redo.
  4. Reload or refresh the WPF prototype/application so that the generated menu and action metadata are read again.

For example, assign ctrl+z to an Undo action in the Edit menu and ctrl+y to a Redo action. The WPF client displays the actions in the menu and handles the configured key combinations when the application has keyboard focus.

Global actions and their menu placement are defined in the model. Framework actions include Save, Refresh, Undo, Redo, Exit, Business Delete, and Do Model. See Documentation:Windows WPF client for how the WPF client builds its menu and UI from ViewModel metadata.

Diagnose shortcuts that stop working

If a configured shortcut does not execute, check whether the active WPF window has lost keyboard focus. A missing Keyboard.FocusedElement means that WPF has no focused input element to receive the key combination.

Add a handler to the WPF window's IsKeyboardFocusWithinChanged event. When the window is active and Keyboard.FocusedElement is null, schedule focus recovery.

this.IsKeyboardFocusWithinChanged += (s, e) =>
{
  if (!this.IsActive)
    return;

  var focusedElement = Keyboard.FocusedElement;
  if (focusedElement == null)
  {
    Trace.WriteLine("IsKeyboardFocusWithinChanged NOTHING");
    RescueNullFocus();
  }
  else
  {
    Trace.WriteLine(
      "IsKeyboardFocusWithinChanged " + focusedElement.GetType().Name);
  }
};

The trace output lets you confirm whether the problem is a missing focus target rather than an incorrectly configured action shortcut.

Restore focus in a WECPOF host

Use a RescueNullFocus method to return focus to an available element in the current WECPOF window environment. Queue the work so that WPF completes its current focus change before the recovery code runs.

private void RescueNullFocus()
{
  new DisplayQueueThis(() =>
  {
    if (Keyboard.FocusedElement != null || !this.IsActive)
      return;

    IInputElement focusTarget = null;

    if (_wecpof.CurrentWindow() != null)
    {
      focusTarget = (_wecpof.CurrentWindow() as FrameworkElement)
        .PredictFocus(FocusNavigationDirection.Right) as IInputElement;
    }

    if (focusTarget == null)
    {
      focusTarget = (_wecpof as FrameworkElement)
        .PredictFocus(FocusNavigationDirection.Right) as IInputElement;
    }

    if (focusTarget != null && !(focusTarget is MenuItem))
      Keyboard.Focus(focusTarget);
  });
}

Call RescueNullFocus() during application startup as well. This covers a startup state where no keyboard focus has been assigned yet.

Menu-item focus gotcha

Do not assign the recovered focus to a MenuItem. A menu item can leave keyboard navigation in the menu instead of returning it to an input control or another usable element. The original focus-recovery pattern attempts to predict another focus target when the first target is a menu item; verify that traversal against your WPF control tree before adding it, because the exact element sequence depends on the host window and its controls.

Put input bindings on the WPF window

When initializing WECPOF, pass the WPF window as the command target. This makes the WPF window own the input bindings instead of the WECPOF environment, which is required for the shortcut handling change described here.

_wecpof.EasyInit(
  _ecospace,
  false,
  pathToStyles,
  thestyle,
  this, // send the WPF window as command target
  _targetgroup);

Apply this in the WPF window that hosts the WECPOF environment. For the baseline setup of a WPF window, menu, and WECPOFWindowEnvironment, see Documentation:Using WECPOF in runtime.

WPF shortcuts and Turnkey web shortcuts

This page applies to a WPF client that hosts WECPOF. Turnkey web applications use their own standard shortcut handling in both AngularJS and Blazor clients. For the current web-client shortcut list, see Documentation:TurnkeyAppStandardShortcutKeys.

See also