如何将键盘输入发送到 OpenGL/DirectX 游戏窗口?

发布于 2024-12-21 19:33:27 字数 90 浏览 2 评论 0原文

如何在游戏窗口中模拟按键(使用 Windows 中的任何编程语言)?

AutoHotKeys 脚本和 .NET SendKeys 功能不起作用...

How can I simulate a keypress in a game window (using any programming language in Windows)?

AutoHotKeys Scripts and .NET SendKeys functions do not work...

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

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

发布评论

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

评论(2

野心澎湃 2024-12-28 19:33:27

使用 AutoIt!3(与 AutoHotKeys 非常相似),您可以使用 Send() (http://www.autoitscript.com/autoit3/docs/functions/Send.htm),但请确保游戏窗口处于活动状态(WinActivate()) 之前。

我已经使用它成功地与《第二人生》(使用 OpenGL)进行交互。您可能需要在模拟按键之间有一个 Sleep() 时间段,因为并非所有游戏都实现良好的键盘缓冲区。

如果这不起作用,则游戏可能会直接访问硬件驱动程序,而您唯一的选择就是连接到键盘驱动程序。

如果游戏是异步轮询,您需要使用 downup 修饰符标志:

Send("{left down}")  ; hold down the LEFT key
Sleep(10)            ; keep it pressed for 10 milliseconds
Send("{left up}")    ; release the LEFT key

计算出按住按键的时间完全取决于您执行程序的频率正在尝试控制正在轮询键盘;没有办法确定。

Using AutoIt!3 (which is quite similar to AutoHotKeys), you'd use Send() (http://www.autoitscript.com/autoit3/docs/functions/Send.htm), but make sure to have the game window active (WinActivate()) before you do.

I've used this to interact with Second Life (which uses OpenGL) succesfully. You may require a Sleep() period between simulated key presses, since not all games implement good keyboard buffers.

If this doesn't work, the game is probably accessing the hardware drivers directly, and your only option is to hook into the keyboard drivers.

If the game is polling asynchonously, you want to use the down and up modifiers flags:

Send("{left down}")  ; hold down the LEFT key
Sleep(10)            ; keep it pressed for 10 milliseconds
Send("{left up}")    ; release the LEFT key

Figuring out how long to keep the key pressed depends entirely on how frequently the program you're trying to control is polling the keyboard; no way to know for sure.

霞映澄塘 2024-12-28 19:33:27

有什么编程语言吗?我的建议是用 C 或 C++ 编写一个小应用程序(尽管您也可以在 .NET 应用程序中使用 P/Invoke< /a>)。

具体来说,您正在寻找 SendInput 来自 Win32 API 的函数,它可以将低级键盘和鼠标输入发送到应用程序。它通过 INPUT 来执行此操作包含您要发送的信息的结构

当然,此函数的使用受 UIPI 的约束,这意味着您要注入输入的应用程序必须以与进行注入的应用程序相同或更低的完整性级别运行。

然而,由于这个函数通常是 SendKeys 在幕后使用的,所以最后一个小警告可能就是它不起作用的原因。很难准确地说;你没有告诉我们你所说的“不工作”是什么意思。

Any programming language? My recommendation is to write up a little app in C or C++ (although you could also do this in a .NET app with P/Invoke).

Specifically, you're looking for the SendInput function from the Win32 API, which can send low-level keyboard and mouse input to an application. It does this in terms of an INPUT structure that contains the information you want to send.

Of course, use of this function is subject to UIPI, which means that the application you're injecting input into must be running at an equal or lesser integrity level than the application that is doing the injecting.

However, since this function is generally what SendKeys uses under the covers, that last little caveat might be why it isn't working. It's hard to say exactly; you don't tell us what you mean by "do not work".

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