通过 ProcessCmdKey 捕获按键序列
我在应用程序中重写 ProcessCmdKey,并且可以使用修饰符(例如 Alt+Ctrl+X)获取任何单个按键。我想要做的是模仿 ReSharper 的快捷方式处理,其中用户按住控制键,然后按 R、M 打开重构对话框
我发现了很多捕获键加修饰符组合的参考,但对于序列却没有太多参考。有这个 Capture multiple key downs in C# 但它使用 KeyDown事件。
还有一些关键的挖掘示例,例如这个 http://www.codeproject.com/KB /system/simple_key_log.aspx 捕获所有内容并使用本机调用。
我是否能够扩展 ProcessCmdKey 来处理按键序列,或者我是否需要寻找其他地方?由于我在 ProcessCmdKey 中捕获了大量快捷方式,如果可能的话,我宁愿不必重新开始
谢谢
I override ProcessCmdKey in my application and can get any single keypress with modifiers (eg. Alt+Ctrl+X). What I want to do is mimic the short cut handling of say ReSharper where the user holds down the control key and then R, M to open the refactor dialog
I have found plenty of references to capture key plus modifier combinations but not much for the sequence. There is this Capture multiple key downs in C# but it uses the KeyDown Event.
There are also key mining examples such as this http://www.codeproject.com/KB/system/simple_key_log.aspx that capture everything and use native calls.
Am I able to extend my ProcessCmdKey to handle the key sequences or do I need to look elsewhere? Since I have a large number of shortcuts captured in ProcessCmdKey I would rather not have to start again if possible
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了实现您想要的功能,您只需跟踪
KeyPress
事件的序列即可。您可以创建一个类来跟踪在
ProcessCmdKey
中按下的最后一个组合键。如果该特定组合与映射命令不匹配,但它是序列的第一个元素,您可以将其存储在类中。然后,下次激活ProcessCmdKey
时,检查新的KeyPressTracker
类以确定序列是否已启动。如果有,则检查新按下的组合键是否是您指定的组合键的第二个元素。请参阅下面的伪代码示例:第 1 步:
ProcessCmdKey
已激活。组合键是 Ctrl+R,这与您要处理的命令不匹配,但它是您要使用的序列的第一个元素 (Ctrl+R+M< /kbd>)。第 2 步:将此按键存储在您创建的新类中,以跟踪最后一次按键。
第 3 步: 第二次激活
ProcessCmdKey
。这次,组合键是 Ctrl+M,它不是我们要查找的按键,而是序列的第二个元素。我们使用新的 KeyPressTracker 类检查上次存储的按键。这将允许您匹配“序列”,例如 Ctrl+R 和 Ctrl+M。In order to achieve the functionality you want you simply need to keep track of the sequence of
KeyPress
events.You can create a class to keep track of the last key combination that was pressed in
ProcessCmdKey
. If that particular combination does not match a mapped command but it is the first element of a sequence you can store it in your class. Then the next timeProcessCmdKey
is activated check your newKeyPressTracker
class to determine if a sequence has been started. If it has then check if the newly pressed key combination is the second element of one you specify. Please see the pseudocode example below:Step 1:
ProcessCmdKey
is activated. The key combination is Ctrl+R, this does not match a command that you want to process but it is the first element of a sequence that you want to use (Ctrl+R+M).Step 2: Store this key-press in a new class you created to keep track of the last key-press.
Step 3:
ProcessCmdKey
is activated a second time. This time, the key combination is Ctrl+M which is not a key-press we're looking for but is the second element of a sequence. We check the last stored keypress using the newKeyPressTracker
class. This will allow you to match a "sequence" such as Ctrl+R and Ctrl+M.