C#DirectInput SendInput不存在';不要影响比赛

本文关键字:影响 SendInput 不存在 C#DirectInput | 更新日期: 2023-09-27 18:19:51

我需要一些关于DirectInput的帮助,我会告诉你我要做什么。我想做的是,当我只按一个键时,我的程序会向游戏发送组合键。示例:我按"r",它会按"1"、"3"、"2"、"4"键。我从这里找到了一些代码。但它们并没有完全起作用。

    public static void Send_Key_Hold(short Keycode)
    {
        INPUT[] InputData = new INPUT[1];
        InputData[0].type = 1;
        InputData[0].ki.wScan = Keycode;
        InputData[0].ki.dwFlags = (int)(KEYEVENTF_SCANCODE);
        SendInput(1, InputData, Marshal.SizeOf(InputData[0]));
    }
    public static void Send_Key_Release(short Keycode)
    {
        INPUT[] InputData = new INPUT[1];
        InputData[0].type = 1;
        InputData[0].ki.wScan = Keycode;
        InputData[0].ki.dwFlags = (int)(KEYEVENTF_KEYUP | KEYEVENTF_SCANCODE);
        SendInput(1, InputData, Marshal.SizeOf(InputData[0]));
    }

这是我的代码和我的问题:当我只使用Send_Key_Hold时,它会按下游戏中的一个键,而其他组合键则不会按下,因为我认为第一个键被按住了。当我同时使用Send_Key_Hold和Send_Key_Release时,它不会在游戏中按下任何按钮。但在桌面上(我指的是除游戏以外的任何其他应用程序),它按下了键。

C#DirectInput SendInput不存在';不要影响比赛

我在互联网上找到了这个例子。当我试着做和你现在一样的事情时,我自己测试了它。

[Flags]
private enum InputType
{
    Mouse = 0,
    Keyboard = 1,
    Hardware = 2
}
[Flags]
private enum KeyEventF
{
    KeyDown = 0x0000,
    ExtendedKey = 0x0001,
    KeyUp = 0x0002,
    Unicode = 0x0004,
    Scancode = 0x0008,
}
[DllImport("user32.dll", SetLastError = true)]
private static extern uint SendInput(uint nInputs, Input[] pInputs, int cbSize);
[DllImport("user32.dll")]
private static extern IntPtr GetMessageExtraInfo();
public static void SendKey(ushort key)
{
    Input[] inputs =
    {
        new Input
        {
            type = (int) InputType.Keyboard,
            u = new InputUnion
            {
                ki = new KeyboardInput
                {
                    wVk = 0,
                    wScan = key,
                    dwFlags = (uint) (KeyEventF.KeyDown | KeyEventF.Scancode),
                    dwExtraInfo = GetMessageExtraInfo()
                }
            }
        }
    };
    SendInput((uint) inputs.Length, inputs, Marshal.SizeOf(typeof (Input)));
}
private struct Input
{
    public int type;
    public InputUnion u;
}
[StructLayout(LayoutKind.Explicit)]
private struct InputUnion
{
    [FieldOffset(0)] public readonly MouseInput mi;
    [FieldOffset(0)] public KeyboardInput ki;
    [FieldOffset(0)] public readonly HardwareInput hi;
}
[StructLayout(LayoutKind.Sequential)]
private struct MouseInput
{
    public readonly int dx;
    public readonly int dy;
    public readonly uint mouseData;
    public readonly uint dwFlags;
    public readonly uint time;
    public readonly IntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
private struct KeyboardInput
{
    public ushort wVk;
    public ushort wScan;
    public uint dwFlags;
    public readonly uint time;
    public IntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
private struct HardwareInput
{
    public readonly uint uMsg;
    public readonly ushort wParamL;
    public readonly ushort wParamH;
}

现在使用SendKey(0x14)将您的T密钥发送到活动窗口(或游戏)。

注意:您需要KeyEventF.Scancode作为您的标志,否则wScan属性将被忽略