gpt4 book ai didi

C# DirectInput SendInput 不影响游戏

转载 作者:太空狗 更新时间:2023-10-29 23:15:40 25 4
gpt4 key购买 nike

我需要一些有关 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 时,它​​不会按下游戏中的任何按钮。但是在桌面上(我指的是任何其他应用程序而不是游戏)它按下了键。

最佳答案

我在网上找到了这个例子。当我尝试做与您现在正在尝试的相同的事情时,我自己对其进行了测试。

[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 属性将被忽略!

关于C# DirectInput SendInput 不影响游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17295374/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com