通过 ProcessCmdKey 捕获按键序列

发布于 2024-12-13 04:15:19 字数 609 浏览 1 评论 0原文

我在应用程序中重写 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 技术交流群。

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

发布评论

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

评论(1

誰認得朕 2024-12-20 04:15:19

为了实现您想要的功能,您只需跟踪 KeyPress 事件的序列即可。

您可以创建一个类来跟踪在 ProcessCmdKey 中按下的最后一个组合键。如果该特定组合与映射命令不匹配,但它是序列的第一个元素,您可以将其存储在类中。然后,下次激活 ProcessCmdKey 时,检查新的 KeyPressTracker 类以确定序列是否已启动。如果有,则检查新按下的组合键是否是您指定的组合键的第二个元素。请参阅下面的伪代码示例:

第 1 步: ProcessCmdKey 已激活。组合键是 Ctrl+R,这与您要处理的命令不匹配,但它是您要使用的序列的第一个元素 (Ctrl+R+M< /kbd>)。

第 2 步:将此按键存储在您创建的新类中,以跟踪最后一次按键。

KeyPressTracker.Store(KeyCode, Modifiers);

第 3 步: 第二次激活 ProcessCmdKey。这次,组合键是 Ctrl+M,它不是我们要查找的按键,而是序列的第二个元素。我们使用新的 KeyPressTracker 类检查上次存储的按键。这将允许您匹配“序列”,例如 Ctrl+RCtrl+M

var lastKeyPress = KeyPressTracker.GetLastKeyPress();

if (lastKeyPress == "Ctrl+R" && currentKeyPress == "Ctrl+M")
{   
    // Show Refactor dialog
}

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 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:

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.

KeyPressTracker.Store(KeyCode, Modifiers);

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 new KeyPressTracker class. This will allow you to match a "sequence" such as Ctrl+R and Ctrl+M.

var lastKeyPress = KeyPressTracker.GetLastKeyPress();

if (lastKeyPress == "Ctrl+R" && currentKeyPress == "Ctrl+M")
{   
    // Show Refactor dialog
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文