如何连接到双击 Ctrl 键?
我正在寻找类似于 Google 桌面的功能。 当您快速按两次 Ctrl 时,会弹出一个小搜索框。
我尝试过使用我在某处找到的相当重的键盘钩子类,但这一直给我带来不可预测的结果(即第一次它会工作,然后我按 Ctrl 一次,它会提高多次按下按键事件?)。
不管怎样,这看起来并不应该很复杂,所以我觉得我一定做错了什么。 我的应用程序(WPF 应用程序)将在主窗口的可见性设置为隐藏的情况下运行,并且不会驻留在任务栏中。 当我执行 Ctrl - Ctrl 时,我想捕获它并显示窗口。
另一个附带问题 - 我目前正在通过注册热键来完成此操作,但这并不能提供我想要的全部功能(双击 Ctrl)。 当窗口显示时,我还没有找到一种 100% 万无一失的方法来检测它何时不再具有焦点(当发生这种情况时我想再次隐藏它)。 我尝试过对“Deactivated”、“LostFocus”等做出响应...并且这些在大多数情况下都有效。 但有时,窗口似乎在没有实际激活或接收焦点的情况下显示。 对此还有什么想法吗?
谢谢!
亚当
What I'm looking for is functionality similar to Google desktop. When you hit Ctrl twice rapidly, a little search box pops up.
I've tried using a fairly heavy keyboard hook class that I found somewhere, but that kept giving meunpredictable results (ie first time it would work, then I'd press Ctrl once, and it would raise the key pressed event several times?).
Anyway, this just doesn't seem like it should be complicated, so I feel like I must be doing something wrong. My app (a WPF app) will be running with the primary window's visibility set to hidden, and not residing in the task bar. When I perform the Ctrl - Ctrl, I want to catch it and show the window.
One other side issue - I'm currently doing this by registering a hot key, but that doesn't give me the full functionality I want (the double-press of Ctrl). When the window is shown, I haven't found a 100% fool-proof way of detecting when it no longer has focus (I want to hide it again when this happens). I've tried responding to Deactivated, LostFocus, etc... and those work most of the time. But sometimes it appears that the window is shown without actually being Activated, or receiving focus. Anyy ideas on this as well?
Thanks!
Adam
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能需要使用 SetWindowsHookEx 来在低级别挂钩键盘事件。
这里有一些 C++ 示例代码,这里是 pinvoke.net 的条目,这应该有助于将其移植到 C#。
对于直接使用的 C# 代码,您可以尝试这个 CodeProject 示例。 根据我的记忆,它在大多数情况下都有效,但有一些错误。 有一次,我协助将其移植到 Jad 引擎,我们在其中实现了一个类 (UserActivityHook)完成了这一任务,并修复了该过程中的一些错误。 这可能是一个非常好的起点......
You'll probably want to use SetWindowsHookEx to hook into the keyboard events at a low level.
Here is some sample code in C++, and here is pinvoke.net's entry, which should help port this to C#.
For C# code, directly usable, you could try this CodeProject sample. From my memory, it worked in most cases, with a few bugs. At one point, I assisted in porting this to the Jad Engine, where we implemented a class (UserActivityHook) that accomplished this, and fixed a few bugs in the process. It might be a very good place to start...
至于第二个问题,如果表单在显示时未激活,您应该调用Form.Activate 显示后。 为了进行简单的调试,请将跟踪语句放入 GotFocus、LostFocus、Enter、Exit 和 Activate、Deactivate 事件中,并查看其中是否有任何一致地满足您的需求。
As for the second question, if the form isn't activated when shown, you should call Form.Activate after you display it. For simple debugging, put trace statements in the GotFocus, LostFocus, Enter, Exit, and Activate, Deactivate events and see if any of these meet your needs consistently.