【问题标题】:How do I read custom keyboard shortcut from user in WPF?如何在 WPF 中读取用户的自定义键盘快捷键?
【发布时间】:2011-01-09 08:03:26
【问题描述】:

在我的应用程序中,我想让用户自定义键盘快捷键,就像在 Visual Studio 的键盘选项中所做的那样。用户可以聚焦空白文本框,然后键入他想要分配给命令的任何快捷方式。

我最接近的方法是订阅 TextBox.PreviewKeyDown 事件,将其设置为已处理以防止在文本框中实际输入文本。然后我忽略与修饰键关联的 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();

}

以上方法适用于以 Ctrl 和 Ctrl+Shift 开头的任何快捷方式,但不适用于任何 Alt 快捷方式。当我按下包含 Alt 的快捷键时,e.Key 始终设置为 Key.System

如何记录用户的 Alt 快捷键?有没有更好、更可靠的方法来记录用户的快捷方式?

【问题讨论】:

  • 我有一种感觉 WPF 使用 Alt 键来尝试捕获 Alt 键组合以专注于与具有助记快捷方式的标签绑定的控件(例如 _File 菜单的 Alt+F ,或 Alt+O 用于 _OK 按钮)。也许有办法阻止或覆盖它?

标签: wpf keyboard-shortcuts user-input


【解决方案1】:

如果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();
}

我将左右 Windows 键添加到修饰符列表中,因为当从终端服务器会话中键入复杂的 (Ctrl+Shift+Alt) 组合键时,它们有时会出现在快捷键名称中。不过,它们从未出现在 Keyboard.Modifiers 中,因为它们是为全局快捷方式保留的,所以我不在那里处理它们。

我还使用了StringBuilder 来避免创建过多的string 实例。

此解决方案适用于任何组合键,Shift+Alt 除外(在这种情况下看不到 Alt 修饰符)。不过,这可能是我的终端服务器环境的产物,因此您的里程可能会有所不同。

最后,我在窗口中添加了_File 菜单,看看会发生什么,Alt+F 快捷键在到达菜单之前就被文本框有效地困住了,这似乎是你想要的。

【讨论】:

  • +1,比我的解决方案更好。不知道 Key key = (e.Key == Key.System ? e.SystemKey : e.Key);技巧:)
【解决方案2】:


如果You used WPF-Command 在您的应用程序中,您可以使用它:

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

【讨论】:

  • 这不能回答问题。问题是如何读取文本框中的组合键,而不是如何在 XAML 中定义热键。
  • 虽然这不能回答原始海报的问题,但这实际上是我通过谷歌搜索偶然发现该线程时正在寻找的答案。所以只是想让其他人知道,在 2016 年,这是一个很好的键绑定整个窗口的解决方案。
猜你喜欢
  • 2012-08-28
  • 1970-01-01
  • 2012-07-16
  • 1970-01-01
  • 2013-02-21
  • 2010-11-24
  • 2012-08-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多