使用直接输入向游戏发送键击

Ibr*_*GUN 6 c#

我可以使用PostMessageapi 发送任何Windows应用程序键击.但是我无法通过使用将关键笔划发送到游戏窗口PostMessage.

任何人都知道使用直接输入功能从C#向游戏发送密钥.

Adr*_*ian 6

另一种方法是直接挂钩DirectInput API - Microsoft Research已经提供了一个库来实现这一点:http://research.microsoft.com/en-us/projects/detours/

一旦您加入API,您就可以随心所欲地做任何事情.但是,值得注意的是,在最新版本的Windows中,鼠标和键盘输入的DirectInput只是Win32 Windows消息的包装.DirectInput在后台生成一个线程,只是截获窗口消息,然后再将它们传递回应用程序.这是微软不再推荐使用DirectInput的原因之一 - 这意味着像PostMessage这样的消息传递API 应该可以正常工作.


Jas*_*son 5

一种替代方法,而不是连接到 DirectX,是使用键盘驱动程序(这不是 SendInput())来模拟按键 - 和事件鼠标按下。

您可以使用Oblita 的 Interception 键盘驱动程序(适用于 Windows 2000 - Windows 7)和C# 库 Interception(包装要在 C# 中使用的键盘驱动程序)。

有了它,你可以做一些很酷的事情,比如:

input.MoveMouseTo(5, 5);
input.MoveMouseBy(25, 25);
input.SendLeftClick();

input.KeyDelay = 1; // See below for explanation; not necessary in non-game apps
input.SendKeys(Keys.Enter);  // Presses the ENTER key down and then up (this constitutes a key press)

// Or you can do the same thing above using these two lines of code
input.SendKeys(Keys.Enter, KeyState.Down);
Thread.Sleep(1); // For use in games, be sure to sleep the thread so the game can capture all events. A lagging game cannot process input quickly, and you so you may have to adjust this to as much as 40 millisecond delay. Outside of a game, a delay of even 0 milliseconds can work (instant key presses).
input.SendKeys(Keys.Enter, KeyState.Up);

input.SendText("hello, I am typing!");

/* All these following characters / numbers / symbols work */
input.SendText("abcdefghijklmnopqrstuvwxyz");
input.SendText("1234567890");
input.SendText("!@#$%^&*()");
input.SendText("[]\\;',./");
input.SendText("{}|:\"<>?");
Run Code Online (Sandbox Code Playgroud)

因为这些按键背后的机制是键盘驱动程序,所以它几乎不受限制。