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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8544271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 18:40:50  来源:igfitidea点击:

How can I send keyboard input to a OpenGL/DirectX game window?

windowsautohotkeysendkeys

提问by user1026187

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

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

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

AutoHotKeys 脚本和 .NET SendKeys 函数不起作用...

回答by Martijn

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.

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

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.

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

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 downand upmodifiers flags:

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

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.

确定按键按下的时间完全取决于您试图控制的程序轮询键盘的频率;没有办法确定。

回答by Cody Gray

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).

任何编程语言?我的建议是用 C 或 C++ 编写一个小应用程序(尽管您也可以在带有P/Invoke的 .NET 应用程序中执行此操作)。

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

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

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.

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

However, since this function is generally what SendKeysuses 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".

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