如何在Windows Forms应用程序中创建复合键盘快捷方式? [英] How do I create compound keyboard shortcuts in a Windows Forms application?

查看:117
本文介绍了如何在Windows Forms应用程序中创建复合键盘快捷方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个组件,使我们能够拥有与任意命令相关的复合键盘快捷键,例如Visual Studio IDE和Microsoft Office.

I want to create a component that allows us to have compound keyboard shortcuts associated with an arbitrary command, like the Visual Studio IDE and Microsoft Office do.

也就是说,键盘快捷键由一系列多次击键组成,例如 Ctrl + W + C .在Visual Studio中,这将打开类视图.当按下第一组键( Ctrl + W )时,消息(Ctrl + W)被按下.正在等待和弦的第二个键... 出现在状态栏中.

That is, keyboard shortcuts consisting of a sequence of multiple keystrokes, such as Ctrl + W + C. In Visual Studio this opens the class-view. When the first set of the keys is pressed (Ctrl + W) the message "(Ctrl + W) was pressed. Waiting for the second key of the chord..." appears in the status bar.

推荐答案

特别是在回答键盘和弦的问题时,我认为目前没有可用的现成选项.点.

In answer to the question of keyboard chords specifically, I do not believe there is a ready-made option available to you at this point.

但是,建模应该足够简单.我将创建一个单一的类,也许是KeyboardChordProvider.它将需要了解表单级别的键盘事件.如其他地方所述,Form.KeyPreview属性必须为true.该提供程序订阅Form.KeyPress事件可能就足够了.如果您以表格形式传递,则可以在提供者的构造函数中完成所有这些操作.

However, it should be simple enough to model. I would create a single class, perhaps KeyboardChordProvider. It will need to know about keyboard events at the form level. As stated elsewhere, the Form.KeyPreview property must be true. It may be enough for this provider to subscribe to the Form.KeyPress event. You could do all of this in the provider's constructor if you passed in the form.

您需要向提供商注册潜在的击键.

You would need to register the potential keystrokes with the provider.

在内部,此实例将跟踪当前状态.每当观察到代表和弦第一个键的击键时,您都将更新提供者的状态并引发事件,以便订户可以设置文本:按下了(CTRL + W).等待和弦的第二个键...

Internally this instance will track the current state. Whenever a keystroke is observed that represents the first key of the chord, you would update the provider's state and raise an event so that a subscriber could set text: (CTRL+W) was pressed. Waiting for second key of chord...

如果下一个击键与可能的辅助选项匹配,则您有一个匹配项,并且可能引发ChordPressed事件,其中包含输入的击键的详细信息.或者,您可以只调用在注册和弦时提供给提供者的特定回调(以避免在ChordPressed事件处理程序中使用switch语句或其他分派).

If the next keystroke matches a potential secondary option, then you have a match and could raise a ChordPressed event containing the details of the strokes entered. Alternatively, you might just call a particular callback that was given to the provider at the time the chord was registered (to avoid having a switch statement or some other dispatch in the ChordPressed event handler).

如果在任何时候按键都不匹配潜在的 next 选项,那么您将重置提供程序的状态.

If, at any time, a keystroke does not match a potential next option, then you would reset the state of the provider.

在提供程序内部,您可以使用树结构对可能的击键进行建模.提供程序的当前状态只是一个特定的树节点.最初,根节点将处于活动状态.如果孩子匹配击键,则它会成为下一个击键的当前节点.如果孩子是叶子节点,则整个和弦都已匹配,您将引发ChordPressed事件(传递使您到达该点的笔画链)或调用存储在叶子中的回调.每当没有按键匹配孩子时,请重置以使根节点处于活动状态.

Internal to the provider you might model the possible keystrokes using a tree structure. The current state of the provider is just a particular tree node. At the beginning, the root node would be active. If a child matches a keystroke then it becomes the current node in anticipation of the next stroke. If the child was a leaf node, then an entire chord has matched and you would raise ChordPressed event (passing the chain of strokes that got you to that point) or invoke the callback stored in the leaf. Whenever no keystroke matches a child, reset back to making the root node active.

我认为这种设计可以实现您想要的.

I think this design would achieve what you want.

这篇关于如何在Windows Forms应用程序中创建复合键盘快捷方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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