问题描述
我在我的应用程序中覆盖 ProcessCmdKey,并且可以使用修饰符(例如 Alt+Ctrl+X)获得任何单个按键.我想要做的是模仿说 ReSharper 的快捷方式处理,其中用户按住 control 键,然后按 R、M 打开重构对话框
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
我找到了很多关于捕获键加修饰符组合的参考,但对于序列而言却不多.有这个 Capture multiple key downs in C# 但它使用 KeyDown事件.
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.
还有关键的挖掘例子,比如这个http://www.codeproject.com/KB/system/simple_key_log.aspx 捕获所有内容并使用本机调用.
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.
我是否能够扩展我的 ProcessCmdKey 来处理按键序列,或者我需要寻找其他地方吗?由于我在 ProcessCmdKey 中捕获了大量快捷方式,因此如果可能,我宁愿不必重新开始
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
谢谢
推荐答案
为了实现您想要的功能,您只需要跟踪 KeyPress
事件的顺序.
In order to achieve the functionality you want you simply need to keep track of the sequence of KeyPress
events.
您可以创建一个类来跟踪在 ProcessCmdKey
中按下的最后一个组合键.如果该特定组合与映射命令不匹配,但它是序列的第一个元素,则可以将其存储在类中.然后在下次激活 ProcessCmdKey
时检查您的新 KeyPressTracker
类以确定序列是否已启动.如果有,则检查新按下的组合键是否是您指定的组合键的第二个元素.请看下面的伪代码示例:
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 time ProcessCmdKey
is activated check your new KeyPressTracker
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:
第 1 步: ProcessCmdKey
被激活.组合键是 ,这与您要处理的命令不匹配,但它是您要使用的序列的第一个元素().
Step 1: ProcessCmdKey
is activated. The key combination is , 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 ().
第 2 步:将此按键存储在您创建的新类中,以跟踪最后一次按键.
Step 2: Store this key-press in a new class you created to keep track of the last key-press.
KeyPressTracker.Store(KeyCode, Modifiers);
第 3 步: ProcessCmdKey
被第二次激活.这一次,组合键是 ,这不是我们要查找的按键,而是序列的第二个元素.我们使用新的 KeyPressTracker
类检查最后存储的按键.这将允许您匹配序列",例如 和 .
Step 3: ProcessCmdKey
is activated a second time. This time, the key combination is 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 new KeyPressTracker
class. This will allow you to match a "sequence" such as and .
var lastKeyPress = KeyPressTracker.GetLastKeyPress();
if (lastKeyPress == "Ctrl+R" && currentKeyPress == "Ctrl+M")
{
// Show Refactor dialog
}
这篇关于通过 ProcessCmdKey 捕获键序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!