简体   繁体   English

如何在WPF中从用户读取自定义键盘快捷键?

[英]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. 在我的应用程序中,我想让用户自定义键盘快捷键,就像在Visual Studio的键盘选项中一样。 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. 我最接近它的工作是订阅TextBox.PreviewKeyDown事件,将其设置为处理以防止在文本框中输入实际文本。 I then ignore the KeyDown events associated with modifier keys (is there a cleaner way to determine if a Key is a modifier key?). 然后我忽略与修饰键相关联的KeyDown事件(是否有更简洁的方法来确定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. 以上适用于以Ctrl和Ctrl + Shift开头的任何快捷方式,但对于任何Alt快捷方式都失败。 The e.Key is always set to Key.System when I press a shortcut containing Alt. 当我按下包含Alt的快捷方式时,e.Key始终设置为Key.System

How can I record Alt shortcuts from the user? 如何记录用户的Alt快捷键? Is there a better, more robust way to record shortcuts form the user? 是否有更好,更健壮的方式来记录用户的快捷方式?

The trick is to use the SystemKey property if the Key property is set to Key.System : 如果Key属性设置为Key.System ,则使用SystemKey属性:

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. 我将左右Windows键添加到修改器列表中,因为当从终端服务器会话中键入复杂( Ctrl+Shift+Alt )组合键时,它们有时会出现在快捷键名称中。 They're never present in Keyboard.Modifiers , though, since they're reserved for global shortcuts, so I don't handle them there. 但它们从未出现在Keyboard.Modifiers ,因为它们是为全局快捷方式保留的,所以我不会在那里处理它们。

I also used a StringBuilder to avoid creating too many string instances. 我还使用StringBuilder来避免创建太多的string实例。

This solution works with any key combination, except Shift+Alt (the Alt modifier is not seen in that case). 此解决方案适用于除Shift+Alt之外的任何组合Shift+Alt (在这种情况下不会看到Alt修饰符)。 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. 最后,我在窗口中添加了一个_File菜单以查看会发生什么,并且Alt+F快捷键在到达菜单之前被文本框有效地捕获,这似乎是您想要的。

Hi 你好
if You used WPF-Command in your application you can use this: 如果You used WPF-Command在应用程序中You used WPF-Command ,则可以使用:

<Window.InputBindings>
  <KeyBinding Command="YourCommnad"
              Gesture="CTRL+C" />
</Window.InputBindings>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM