Simulating Keyboard with SendInput C#

假装没事ソ 提交于 2019-12-13 18:48:06

问题


I tried to simulate a key from the keyboard for a direct x game with this code:

public static void Send(short Keycode)
{
    INPUT[] InputData = new INPUT[1];

    InputData[0].type = 1;
    InputData[0].ki.wScan = Keycode;
    InputData[0].ki.dwFlags = KEYEVENTF_KEYUP | KEYEVENTF_SCANCODE;
    InputData[0].ki.time = 0;
    InputData[0].ki.dwExtraInfo = IntPtr.Zero;

    SendInput(1, InputData, Marshal.SizeOf(typeof(INPUT)));
}

The problem is, this does not simulate the key. The key isn't pressed.


回答1:


You need to send a KEYEVENTF_KEYDOWN and then a KEYEVENTF_KEYUP event in order for the game to correctly process the key events.

Leave a little pause in between the sending of the key events.

This is needed if the game is polling the keys, it then has time to recognize the pressed key.



来源:https://stackoverflow.com/questions/30621624/simulating-keyboard-with-sendinput-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!