使用SendInput C#模拟键盘 [英] Simulating Keyboard with SendInput C#

查看:657
本文介绍了使用SendInput C#模拟键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用以下代码从直接X游戏的键盘上模拟一个按键:

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.

推荐答案

您需要先发送一个KEYEVENTF_KEYDOWN事件,然后发送一个KEYEVENTF_KEYUP事件,以便游戏正确处理关键事件.

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.

这篇关于使用SendInput C#模拟键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆