不使用 DLL 的键盘挂钩?

发布于 2024-12-22 04:34:08 字数 168 浏览 2 评论 0 原文

我正在尝试创建一个带有一些按键绑定(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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

_失温 2024-12-29 04:34:08

虽然正如 klartex 所说,低级键盘/鼠标钩子不需要 DLL(与所有其他类型的钩子不同),但任何类型的钩子对于您想要完成的任务来说肯定是大材小用。

您所需要的只是RegisterHotKey 功能,它允许您将任何键(或键组合)注册为系统范围的热键。这满足了您即使在应用程序失去焦点时也能够拾取按键的要求。

作为奖励,RegisterHotKey 不需要 DLL,也不像系统范围的钩子那样“重”。 Hooks 对性能有负面影响;您不应该在 RegisterHotKey 中看到这一点。

通过调用该函数注册热键后,您可以处理 应用程序窗口过程中的 WM_HOTKEY 消息。完成后,请确保调用 UnregisterHotKey 函数 取消注册您的应用程序以处理该热键。

文档中提到了这里唯一的警告:

F12 键保留供调试器随时使用,因此不应将其注册为热键。即使您没有调试应用程序,F12 也会被保留,以防驻留内核模式调试器或即时调试器。

但如果您安装低级键盘挂钩,也会出现同样的问题。无论应用程序如何,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 with RegisterHotKey.

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:

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.

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.

舂唻埖巳落 2024-12-29 04:34:08

有了这个,你应该能够在 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

暮色兮凉城 2024-12-29 04:34:08

RegisterHotKey 不需要 dll。

RegisterHotKey does not require a dll.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文