Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I read custom keyboard shortcut from user in WPF?

In my application, I want to let users customize keyboard shortcuts, just like it's done in Visual Studio's keyboard options. The user can focus a blank text box and then type any shortcut he wants to assign to a command.

The closest I've come to make it work is by subscribing to the TextBox.PreviewKeyDown event, setting it as handled to prevent actual text input in the text box. I then ignore the KeyDown events associated with modifier keys (is there a cleaner way to determine if a Key is a modifier key?).

// Code-behind
private void ShortcutTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
    // The text box grabs all input
    e.Handled = true;

    if (e.Key == Key.LeftCtrl || 
        e.Key == Key.RightCtrl || 
        e.Key == Key.LeftAlt ||
        e.Key == Key.RightAlt || 
        e.Key == Key.LeftShift ||
        e.Key == Key.RightShift)
        return;

    string shortcutText = "";
    if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
        shortcutText += "Ctrl+";
    if ((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift)
        shortcutText += "Shift+";
    if ((Keyboard.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt)
        shortcutText += "Alt+";
    _ShortcutTextBox.Text = shortcutText + e.Key.ToString();

}

The above works for any shortcut starting with Ctrl and Ctrl+Shift, but fails for any Alt shortcuts. The e.Key is always set to Key.System when I press a shortcut containing Alt.

How can I record Alt shortcuts from the user? Is there a better, more robust way to record shortcuts form the user?

like image 728
Anthony Brien Avatar asked Jan 25 '10 23:01

Anthony Brien


People also ask

How do I search for a shortcut in Visual Studio?

Keyboard shortcuts in Visual Studio No matter which profile you chose, you can identify the shortcut for a command by opening the Options dialog box, expanding the Environment node, and then choosing Keyboard. You can also customize your shortcuts by assigning a different shortcut to any given command.

How can you enable the user to access a menu item from the keyboard?

If the selected control is a button, to activate it, press Spacebar or Enter. If the selected control is a split button (that is, a button that opens a menu of additional options), to activate it, press Alt+Down arrow key. Press the Tab key to browse the options. To select the current option, press Spacebar or Enter.

How can I see the shortcut key in VS Code?

You can view the currently active keyboard shortcuts in VS Code in the Command Palette (View -> Command Palette) or in the Keyboard Shortcuts editor (File > Preferences > Keyboard Shortcuts).

How do I run a shortcut code?

use shortcut Ctrl + Alt + M. or press F1 and then select/type Stop Code Run.


2 Answers

Hi
if You used WPF-Command in your application you can use this:

<Window.InputBindings>
  <KeyBinding Command="YourCommnad"
              Gesture="CTRL+C" />
</Window.InputBindings>
like image 160
Rev Avatar answered Sep 30 '22 05:09

Rev


The trick is to use the SystemKey property if the Key property is set to Key.System:

private void ShortcutTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
    // The text box grabs all input.
    e.Handled = true;

    // Fetch the actual shortcut key.
    Key key = (e.Key == Key.System ? e.SystemKey : e.Key);

    // Ignore modifier keys.
    if (key == Key.LeftShift || key == Key.RightShift
        || key == Key.LeftCtrl || key == Key.RightCtrl
        || key == Key.LeftAlt || key == Key.RightAlt
        || key == Key.LWin || key == Key.RWin) {
        return;
    }

    // Build the shortcut key name.
    StringBuilder shortcutText = new StringBuilder();
    if ((Keyboard.Modifiers & ModifierKeys.Control) != 0) {
        shortcutText.Append("Ctrl+");
    }
    if ((Keyboard.Modifiers & ModifierKeys.Shift) != 0) {
        shortcutText.Append("Shift+");
    }
    if ((Keyboard.Modifiers & ModifierKeys.Alt) != 0) {
        shortcutText.Append("Alt+");
    }
    shortcutText.Append(key.ToString());

    // Update the text box.
    _ShortcutTextBox.Text = shortcutText.ToString();
}

I added the left and right Windows keys to the modifier list, because they sometimes appeared in the shortcut key name when a complex (Ctrl+Shift+Alt) key combination was typed from a Terminal Server session. They're never present in Keyboard.Modifiers, though, since they're reserved for global shortcuts, so I don't handle them there.

I also used a StringBuilder to avoid creating too many string instances.

This solution works with any key combination, except Shift+Alt (the Alt modifier is not seen in that case). That might be an artifact of my Terminal Server environment, though, so your mileage may vary.

Finally, I added a _File menu to the window to see what would happen, and the Alt+F shortcut key is effectively trapped by the text box before it reaches the menu, which seems to be what you want.

like image 39
Frédéric Hamidi Avatar answered Sep 30 '22 05:09

Frédéric Hamidi