我正在尝试创建一个带有一些按键绑定(F1-F12)的程序,该程序将在失焦时(特别是在游戏运行时)拾取按键。
有没有办法在不使用全局挂钩的情况下检测这些按键?我正在编程的语言(real studio)没有创建DLL(全局挂钩所需)的方法,另外,我希望它能够与mac跨平台(这就是realstudio所做的)。
I'm trying to create a program with some key bindings (F1-F12) which will pick up the key presses while out of focus (specifically, while a game is running).
Is there anyway to detect these key presses without using a global hook? The language I am programming in (real studio) doesn't have a means of creating a DLL (required for a global hooks), plus, I'm hoping of having it cross platform with mac (which is what realstudio does).
发布评论
评论(3)
虽然正如 klartex 所说,低级键盘/鼠标钩子不需要 DLL(与所有其他类型的钩子不同),但任何类型的钩子对于您想要完成的任务来说肯定是大材小用。
您所需要的只是
RegisterHotKey
功能,它允许您将任何键(或键组合)注册为系统范围的热键。这满足了您即使在应用程序失去焦点时也能够拾取按键的要求。作为奖励,
RegisterHotKey
不需要 DLL,也不像系统范围的钩子那样“重”。 Hooks 对性能有负面影响;您不应该在RegisterHotKey
中看到这一点。通过调用该函数注册热键后,您可以处理 应用程序窗口过程中的
WM_HOTKEY
消息。完成后,请确保调用UnregisterHotKey
函数 取消注册您的应用程序以处理该热键。文档中提到了这里唯一的警告:
但如果您安装低级键盘挂钩,也会出现同样的问题。无论应用程序如何,F12 都不是一个好的候选热键。如果您绝对必须使用它,请自行承担风险。
While as klartex says, a low-level keyboard/mouse hook does not require a DLL (unlike all other types of hooks), a hook of any kind is certainly overkill for what you're trying to accomplish.
All you need is the
RegisterHotKey
function, which allows you to register any key (or combination of keys) as a system-wide hotkey. This fulfills your requirement of being able to pick up the key presses even while your application is out of focus.As a bonus,
RegisterHotKey
does not require a DLL nor is it as "heavy" as a system-wide hook. Hooks have a negative effect on performance; you shouldn't see that withRegisterHotKey
.Once you've registered a hot key by calling the function, you handle
WM_HOTKEY
messages inside of your application's window procedure. Once you're finished, make sure that you call theUnregisterHotKey
function to unregister your application as handling that hot key.The only caveat here is mentioned in the documentation:
But the same issue would apply if you were installing a low-level keyboard hook. F12 is just not a good candidate hot key, regardless of the application. If you absolutely must, use it at your own risk.
有了这个,你应该能够在 Windows 上挂钩没有 DLL 的键盘: http://msdn.microsoft.com/en-us/library/windows/desktop/ms644985%28v=vs.85%29.aspx
With this you should be able to hook keyboard without DLL on windows: http://msdn.microsoft.com/en-us/library/windows/desktop/ms644985%28v=vs.85%29.aspx
RegisterHotKey
不需要 dll。RegisterHotKey
does not require a dll.