C# 应用程序中的全局键盘捕获
我想在我的应用程序中捕获键盘快捷键,并在用户即使在应用程序外部按下键盘组合时也会触发一个对话框。 类似于Google桌面搜索的Ctrl,Ctrl可以调出搜索对话框。
我尝试过使用一些键盘钩子模块,这些模块基本上使用 Win32 互操作来获得这种效果,但是我尝试过的每个实现都在一定程度上限制了键盘,当应用程序执行密集操作时,您会开始出现奇怪的行为。 比如加载大量数据,这会导致键盘和鼠标死机。
我正在寻找一种轻量级的解决方案,可以在不束缚键盘和鼠标的情况下完成此操作。
I want to capture a keyboard shortcut in my application and trigger a dialog to appear if the user presses a keyboard combo even outside of the app. Similar to Google Desktop Search's Ctrl, Ctrl to bring up the search dialog.
I have tried using some keyboard hook modules out there that basically use Win32 interop to get this effect but each implementation I've tried ties down the keyboard to some extent to where you start getting weird behaviors when the application is doing something intensive. Such as loading a large amount of data, this would cause the keyboard and mouse to lockup.
I'm looking for a lightweight solution that would allow this to be done without tying down the keyboard and mouse.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
Stephen Toub 写了一篇很棒的文章< /a> 在 C# 中实现全局键盘挂钩:
Stephen Toub wrote a great article on implementing global keyboard hooks in C#:
这是我的有效代码:
用法:
Here's my code that works:
Usage:
根据 dube 的要求,我正在发布我的修改版本 Siarhei Kuchuk 的回答。
如果您想检查我的更改,请搜索
// EDT
。 我已经评论了其中的大部分内容。设置
使用 可以在此处看到差异
感谢Siarhei Kuchuk 他的帖子。 尽管我简化了用法,但这个初始代码对我来说非常有用。
As requested by dube I'm posting my modified version of Siarhei Kuchuk's answer.
If you want to check my changes search for
// EDT
. I've commented most of it.The Setup
The Usage differences can be seen here
Thanks to Siarhei Kuchuk for his post. Even tho I've simplified the usage this initial code was very useful for me.
如果全局热键就足够了,那么 RegisterHotKey 就可以了
If a global hotkey would suffice, then RegisterHotKey would do the trick
我的代表太低,无法发表评论,但关于
CallbackOnCollectedDelegate
异常,我修改了 C4d 中的public void SetupKeyboardHooks()
em> 答案看起来像这样:其中
GcSafeHookProc
只是 OP 中_hookProc
的公共 getter,并将
hookProc
作为私有字段存储在类调用SetupKeyboardHooks(...)
,因此保持引用处于活动状态,避免垃圾回收,不再出现CallbackOnCollectedDelegate
异常。 似乎在 GlobalKeyboardHook 类中添加此附加引用还不够。 也许请确保在关闭应用程序时也处理此引用。My rep is too low to comment, but concerning the
CallbackOnCollectedDelegate
exception, I modified thepublic void SetupKeyboardHooks()
in C4d's answer to look like this:where
GcSafeHookProc
is just a public getter for_hookProc
in OPsand stored the
hookProc
as a private field in the class calling theSetupKeyboardHooks(...)
, therefore keeping the reference alive, save from garbage collection, no moreCallbackOnCollectedDelegate
exception. Seems having this additional reference in theGlobalKeyboardHook
class is not sufficient. Maybe make sure that this reference is also disposed when closing your app.对于系统全局组合键和多键快捷键,此处非常简单。 类代码:
以及如何在表单应用程序中使用它:
以及控制台应用程序的非常简单的示例:
我还建议阅读这篇文章:
https://stackoverflow.com/a/46014022/4238323
重要说明:
系统范围的钩子非常危险,你必须非常小心你在做什么。 如果您使用此方法挂钩关键事件,请确保正确取消挂钩并释放内存,这可能很困难,特别是在控制台应用程序中。 我遇到了几个蓝页和一些系统不稳定的情况,例如关机和睡眠问题以及使用几个小时后冻结。 所以使用时请格外小心。
For system global key combinations and multi-key short-keys there is very easy one here. Class code:
And how to use it in form application:
And very simple example for console application:
I also recommend to read this post :
https://stackoverflow.com/a/46014022/4238323
Important Note:
System wide hooks are extremely dangerous you MUST be very careful about what are you doing. If you hook key event using this method make sure unhook and release memory correctly which It could be hard specially in console application. I encountered several Blue-Pages and some system instability such as shutdown and sleep problems and freeze-up after some hours of using this. So please take extra care while using it.
然后工作正常。
And then is working fine.