不使用DLL的键盘挂钩? [英] Keyboard hooks without using a DLL?
问题描述
我正在尝试创建一个具有某些键绑定(F1-F12)的程序,该程序将在焦点不清晰时(特别是在游戏运行时)拾取按键.
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).
有没有使用全局钩子来检测这些按键的方法?我在(real studio)中编程的语言没有创建DLL(全局挂钩所需)的方法,此外,我希望它可以与mac跨平台使用(这是realstudio的工作). /p>
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).
推荐答案
正如klartex所说,低级键盘/鼠标挂钩不需要DLL(不同于所有其他类型的挂钩),它是挂钩当然,任何一种都对您要完成的工作构成了过大杀伤力.
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.
所有您需要的是 RegisterHotKey
函数 ,它使您可以将任何键(或键组合)注册为系统范围的热键.这样可以满足您即使您的应用程序不在焦点时也能够拿起按键的要求.
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.
作为奖励,RegisterHotKey
不需要DLL,也不象系统范围的钩子一样繁重".挂钩对性能有负面影响;您不应该在RegisterHotKey
上看到它.
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 with RegisterHotKey
.
一旦通过调用函数注册了热键,就可以处理 UnregisterHotKey
功能来取消您的应用程序注册,以处理该热键.
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 the UnregisterHotKey
function to unregister your application as handling that hot key.
文档中仅提及此处的警告:
The only caveat here is mentioned in the documentation:
F12密钥始终保留供调试器使用,因此不应将其注册为热键.即使在不调试应用程序的情况下,如果驻留了内核模式调试器或即时调试器,F12也会保留.
The F12 key is reserved for use by the debugger at all times, so it should not be registered as a hot key. Even when you are not debugging an application, F12 is reserved in case a kernel-mode debugger or a just-in-time debugger is resident.
但是,如果您安装低级键盘挂钩,则同样的问题也将适用.无论应用如何, F12 都不是一个好的候选热键.如果绝对必须使用,则后果自负.
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.
这篇关于不使用DLL的键盘挂钩?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!