如何在WPF中设置shotkeys [英] How to setup shotkeys in WPF

查看:61
本文介绍了如何在WPF中设置shotkeys的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在使用带菜单的WPF应用程序。

我需要设置一些短按键那:



Ctrl + D,Ctrl + O,...



我不怎么样我点击我的菜单项时调用相同的事件处理程序。



感谢您帮助

解决方案

除了解决方案2之外,我想解释如何处理低级别的键盘事件,这在某些自定义情况下非常有用,这些情况无法简化为已知的手势。此外,您需要了解重用不同事件处理程序的想法。



让我们从相同的事件处理程序开始。您可以使用常规(命名,非匿名)方法在任何处理程序中调用。这样,您可以在两个或多个不同的事件处理程序中调用相同的函数。最好从事件(事件参数等)中提取这些函数,使它们纯粹是语义的。很简单,不是吗?



现在,Ctrl-something键事件可以作为 KeyDown 事件处理或者任何 UIElement 。但是,这会产生一个问题。也许,无论何时按下这些键,您都希望在表单中处理Ctrl键事件。如果您在表单上有一个表单和几个控件,那么这些控件中的任何一个都可以获得键盘焦点。这个UI元素不处理键盘元素,事件将被忽略。



如何排序?很高兴理解事件可以使用隧道路径冒泡路径 路由。请参阅: http://msdn.microsoft.com/en-us /library/ms742806%28v=vs.110%29.aspx [ ^ ]。



此CodeProject文章也可以使用:冒泡或隧道基本WPF事件 [ ^ ]。



正如您所看到的,在您的情况下,它可以像这样简单:处理表单的事件 PreviewKeyDown 。例如:

 PreviewKeyDown + =(sender,eventArgs)= >  {
< span class =code-comment> // 仅在按下Control时允许处理,不与
组合 // 其他修饰键:
if (键盘。 Modifiers!= ModifierKeys.Control) return ;
if (eventArgs.Key == Key.D)
CtrlDHandler();
if (eventArgs.Key == Key.O)
CtrlOHandler();
// ...
// 您可能需要阻止进一步的事件传播:
eventArgs.Handled = true < /跨度>;
};



这里,方法 CtrlDHandler CtrlOHandler 可以是在其他事件处理程序中重用的非常语义,真实的事件处理程序。请注意我使用的匿名甚至处理程序的好处。通过这种方式,您可以释放所有不需要和未使用的详细信息的真实处理程序,例如 sender eventArgs 。现在,这些方法可以用在与菜单项相关的处理程序中,但最好使用命令。请参阅:

http:// msdn .microsoft.com / zh-CN / library / ms752308%28v = vs.110%29.aspx [ ^ ],

,例如:

< a href =http://www.wpf-tutorial.com/common-interface-controls/menu-control> http://www.wpf-tutorial.com/common-interface-controls/menu-control [ ^ ],

http://www.skimedic .com / blog / post / 2008/07/14 / WPF-Menus-and-Commands.aspx [ ^ ]。



-SA


您可以将InputGestureText用于C trl + D,Ctrl + O,....并且可以使用命令绑定菜单项,例如

 <   MenuItem    名称  =  itmOpen   标题  =  _打开    InputGestureText   =  Ctrl + D   命令  = 打开 >  
< < span class =code-leadattribute> MenuItem.CommandBindings >
< CommandBinding < span class =code-attribute>命令 = 打开 已执行 = CommandBinding_Executed / >
< / MenuItem.CommandBindings >
< / MenuItem >


Hi everyone,

I'm working on a WPF application with menu.
I need to set up some short keys like that:

Ctrl + D, Ctrl + O, ...

I don't how to do to call the same event handler called when I click on my menu item.

Thanks for Helping

解决方案

In addition to Solution 2, I want to explain how you can handle keyboard events on low level, which can be useful in some custom situations, which cannot be reduced to known "gestures". Besides, you need to understand the idea of reusing the handlers of different events.

Let's start from the "same event handler". You can use a regular (named, not anonymous) method to call in any handler. This way, you can call the same function in two or more different event handlers. It's better to make those functions abstracted from the events (event arguments, etc), make them purely semantic. Simple, isn't it?

Now, Ctrl-something key events can be handled as KeyDown event or any UIElement. However, it would make one problem. Probably, you want you Ctrl-key event to be handled in your form no matter when you hit those keys. If you have, say, a form and several controls on a form, any of those controls can grab the keyboard focus. It this UI element does not handle the keyboard element, the event will be ignored.

How to sort it out? It's good to understand that events can be routed using a tunneling route or a bubbling route. Please see: http://msdn.microsoft.com/en-us/library/ms742806%28v=vs.110%29.aspx[^].

This CodeProject article can also be used: To bubble or tunnel basic WPF events[^].

As you can see, in your case, it can be as simple as this: handle the event PreviewKeyDown of the form. For example:

PreviewKeyDown += (sender, eventArgs) => {
    // allow handling only when Control is pressed, no combinations with
    // other modifier keys:
    if (Keyboard.Modifiers != ModifierKeys.Control) return;
    if (eventArgs.Key == Key.D)
        CtrlDHandler();
    if (eventArgs.Key == Key.O)
        CtrlOHandler();
    // ...
    // you may need to block further event propagation:
    eventArgs.Handled = true;
};


Here, the methods CtrlDHandler and CtrlOHandler can be those very "semantic", "real" event handlers reused in other event handlers. Note the benefit of the anonymous even handlers I used. This way, you can free up those "real" handlers of all the unwanted and unused detail, such as sender and eventArgs. Now, these methods could be used in handlers associated with menu items, but it would be better to use commands. Please see:
http://msdn.microsoft.com/en-us/library/ms752308%28v=vs.110%29.aspx[^],
and for example:
http://www.wpf-tutorial.com/common-interface-controls/menu-control[^],
http://www.skimedic.com/blog/post/2008/07/14/WPF-Menus-and-Commands.aspx[^].

—SA


You can use InputGestureText for Ctrl + D, Ctrl + O, .... and can bind menu item with command like

<MenuItem Name="itmOpen" Header="_Open" InputGestureText="Ctrl+D" Command="Open">
      <MenuItem.CommandBindings>
            <CommandBinding Command="Open" Executed="CommandBinding_Executed" />
      </MenuItem.CommandBindings>
 </MenuItem>


这篇关于如何在WPF中设置shotkeys的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆