如何在 WPF 中抑制菜单键盘快捷键 KeyDown 处理?

发布于 2024-12-07 20:14:55 字数 747 浏览 1 评论 0原文

我有一个具有菜单键盘快捷键和文本框的应用程序。我希望当文本框获得焦点时禁用键盘快捷键,但我找不到一种简单的方法来做到这一点。我可以处理文本框的 PreviewKeyDown 事件,但发送 KeyDown 事件不会导致 TextInput 事件触发,因此我必须自己手动触发 TextInput 事件,并且必须确保每个文本框都会覆盖PreviewKeyDown 事件并创建 TextInput 事件。

当文本框具有焦点时,这是抑制菜单键盘快捷键的唯一方法还是有其他不易出错的方法?

编辑:

以下是我添加键盘快捷键的方法:

var kgc = new NuiWpfCore.Input.UnrestrictedKeyGestureConverter();  // allows gestures without modifier keys
var result = kgc.ConvertFromString(s) as NuiWpfCore.Input.UnrestrictedKeyGesture;
m_KeyBinding = new KeyBinding();
m_KeyBinding.Command = KeyBindingCommand;
m_KeyBinding.Modifiers = result.Modifiers;
m_KeyBinding.Key = result.Key;
m_Parent.InputBindings.Add(m_KeyBinding); // m_Parent is of type UIElement

I have an application that has menu keyboard shortcuts and text boxes. I want the keyboard shortcuts to be disabled when the text box has focus, but I can't figure out an easy way to do this. I could handle the text box's PreviewKeyDown event, but sending a KeyDown event doesn't cause the TextInput event to trigger so I'd have to manually trigger the TextInput event myself, and I'd have to make sure that every text box overrides the PreviewKeyDown event and creates a TextInput event.

Is this the only way suppress menu keyboard shortcuts when a textbox has focus or is there another way that is less error prone?

EDIT:

Here's how I'm adding the keyboard shortcut:

var kgc = new NuiWpfCore.Input.UnrestrictedKeyGestureConverter();  // allows gestures without modifier keys
var result = kgc.ConvertFromString(s) as NuiWpfCore.Input.UnrestrictedKeyGesture;
m_KeyBinding = new KeyBinding();
m_KeyBinding.Command = KeyBindingCommand;
m_KeyBinding.Modifiers = result.Modifiers;
m_KeyBinding.Key = result.Key;
m_Parent.InputBindings.Add(m_KeyBinding); // m_Parent is of type UIElement

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无力看清 2024-12-14 20:14:55

您能否提供更多输入,例如您如何注册键盘快捷键?使用KeyBinding?如果是这样,它已经需要指定Command。因此,如果文本框处于焦点状态,则在命令的 Canexecute 中返回 false。

这将禁用键盘快捷键。您这边的一些源代码可能会有用。

编辑

现在你已经有了使用KeyBindingCommandKeyBinding,它对我来说看起来像一个RoatedCommand。如果是这样,则命令绑定具有 CanExecute 功能。

    m_Parent.CommandBindings.Add(new CommandBinding(KeyBindingCommand, OnExecuted, CanExcute));

CanExecute 处理程序中...CanExecutedRoulatedArgs 可能/可能不正确...

    private void CanExecute(object sender, CanExecutedRoutedArgs args)
    {
          e.CanExecute = !textBox.IsFocused;
    } 

上面的代码仅用于说明。

Can you provide more inputs as in HOW are you registering the Keyboard shortcuts? Using KeyBinding? If so it already needs Command specified. So in the Canexecute of the command return false if the textbox is in focus.

This will disable keyboard shortcuts. Some source ocde from your side might be useful.

EDIT

SO now that you have KeyBinding using KeyBindingCommand which look like a RoutedCommand to me. If so do command bindings having CanExecute function.

    m_Parent.CommandBindings.Add(new CommandBinding(KeyBindingCommand, OnExecuted, CanExcute));

In the CanExecute handler.... CanExecutedRoutedArgs may / may not be correct...

    private void CanExecute(object sender, CanExecutedRoutedArgs args)
    {
          e.CanExecute = !textBox.IsFocused;
    } 

The code above is only for illustration.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文