捕捉“全局热键” (视窗)
现在,我一直在尝试创建与 Push-To-Talk 语音聊天应用程序类似的功能,但到目前为止我找不到任何合适的解决方案。我没有使用 MFC 或 CLR。
问题很简单。我的窗口通常应该失焦(即最小化等),但我需要对按键做出反应(基本上,我什至不想知道按钮是否被按下)。不幸的是,WM_KEYDOWN 仅在窗口具有键盘焦点时才有效。我确实知道例如 Teamspeak 使用 DirectInput 来实现此目的,但我也知道没有它也绝对可以完成,这是我非常喜欢的。
我可以使用的唯一其他解决方案是使用 GetAsyncKeyState 进行轮询,但看起来这也远不是一个好的解决方案。如果可能的话,我仍然更喜欢使用 Windows 消息。
Now I've been trying to create a similar functionality as there is in Push-To-Talk voice chat applications, but so far I couldn't find any fitting solutions to this. I am not using MFC or CLR.
The problem is quite simple. My window should be usually out of focus (ie minimized etc), but I need to react to keypresses (basically, I don't even want to know if the button is being held down or not). Unfortunately, WM_KEYDOWN only works if the window has keyboard focus. I do know that for example Teamspeak uses DirectInput for this, but I'm also aware that it can definitely be done without it as well, which I'd highly prefer.
The only other solution that I could make work is polling with GetAsyncKeyState, but it looks like this is far from being a good solution either. If at all possible, I'd still prefer using Windows messages.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该问题可以通过
RegisterHotKey 解决
或使用 全局低级键盘钩子。RegisterHotKey
(Cody Gray 在推荐中建议的)可能是更合适的选择。The problem can be solved either with
RegisterHotKey
or with a global low-level keyboard hook.RegisterHotKey
(which Cody Gray suggested in the commends) is probably the more suitable choice here.我同意乔恩的观点。使用 user32.dll 和 RegisterHotKey 函数。非常有用的示例代码位于:https://www.codeproject。 com/kb/cs/kiosk_cs.aspx?display=print
如果像我最初一样,代码看起来完全是乱码,并且您不知道该怎么做,请观看教程:https://www.youtube.com/watch?v=qQWqGOaZiFI 他们非常 有用!
如果您尝试注册系统热键:但是请注意,第一个链接中的代码将返回错误,具体取决于您尝试注册的系统热键。我的问题突出了这个问题:你怎么办禁用user32.dll中的系统热键?(目前未解决)。那里还有一个代码编辑,可以修复您在尝试覆盖系统热键时遇到的错误之一:
我确实设法阻止了Windows + E热键,但其他诸如 Alt + F4 和 Home + Right 不会被禁用,并且会返回错误。
如果您不尝试覆盖系统热键,那么这应该不会成为您的问题。不过,如果您的热键之一无法注册,我建议您使用下面的代码来诊断您的代码。
^ 该行代码的完整上下文位于第一个链接中!
I agree with Jon. Use user32.dll and the RegisterHotKey function. Very useful example code is located at: https://www.codeproject.com/kb/cs/kiosk_cs.aspx?display=print
If, like I initially was, the code looks like complete gibberish and you have no idea what to do, watch the tutorials: https://www.youtube.com/watch?v=qQWqGOaZiFI They're very useful!
If you try to register a system hotkey: However note that the code from the first link would return an error, depending on what system hotkey you try to register. My question on this which highlights the issue: How do you disable system hotkeys in user32.dll? (currently unsolved). There is also a code edit there that will fix one of the errors you will get, if you try to override system hotkeys:
I did manage to block the Windows + E hotkey, but others like Alt + F4 and Home + Right did not get disabled and would return an error.
This should not be an issue for you if you do not try to override a system hotkey. I would however suggest you use the code below to diagnose your code, if one of your hotkeys fails to register.
^ Full context on that line of code is in the first link!