本文介绍了C#的DirectInput SendInput不会影响到游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要有关DirectInput的一些帮助,我会告诉什么,我试图做的。我要尽我的程序发送键组合到一个游戏,当我只按了一个键。 Examp:我会按R,它会PRES1,3,2,4键。我发现一些代码从这里开始。但是,他们并没有完全奏效。



公共静态无效Send_Key_Hold(短键码)
{
输入[] = InputData新的输入[1];
InputData [0] .TYPE = 1;
InputData [0] = .ki.wScan个人识别号码;
InputData [0] = .ki.dwFlags(INT)(KEYEVENTF_SCANCODE);

SendInput(1,InputData,Marshal.SizeOf(InputData [0]));
}
公共静态无效Send_Key_Release(短键码)
{
输入[] = InputData新的输入[1];
InputData [0] .TYPE = 1;
InputData [0] = .ki.wScan个人识别号码;
InputData [0] = .ki.dwFlags(INT)(KEYEVENTF_KEYUP | KEYEVENTF_SCANCODE);

SendInput(1,InputData,Marshal.SizeOf(InputData [0]));
}

下面是我的代码和我的问题:当我使用Send_Key_Hold只有它在按下未按因为第一个关键是holded者我觉得游戏和其他组合键一个键。当我使用Send_Key_Hold和Send_Key_Release一起不按游戏上的任何按钮。但在台式机(我的意思是电气特性的应用程序不是游戏)它按下键。


解决方案

我发现互联网上的这个例子。测试了我自己,当我试图做,因为你现在想同样的事情。

  [国旗] 
私人枚举的inputType
{
鼠标= 0,
键盘= 1,
硬件= 2
}

[国旗]
私人枚举KeyEventF
{
的KeyDown = 0×0000,
ExtendedKey = 0×0001,
的KeyUp = 0×0002,
的Unicode = 0x0004单元,
扫描码= 0×0008,
}

函数[DllImport(user32.dll中,SetLastError = TRUE)]
私人静态外部UINT SendInput(UINT nInputs,输入[] pInputs,诠释CBSIZE);

函数[DllImport(user32.dll中)]
私人静态外部的IntPtr GetMessageExtraInfo();

公共静态无效SendKey(USHORT键)
{
输入[]输入=
{
新的输入
{
类型=(INT)InputType.Keyboard,
U =新InputUnion
{
琪=新KeyboardInput
{
WVK = 0,
wScan =键,
的dwFlags =(UINT)(KeyEventF.KeyDown | KeyEventF.Scancode),
dwExtraInfo = GetMessageExtraInfo()
}
}
}
};

SendInput((UINT)inputs.Length,投入,Marshal.SizeOf(typeof运算(输入)));
}

私人结构输入
{
公众诠释类型;
公共InputUnion U;
}

[StructLayout(LayoutKind.Explicit)]
私人结构InputUnion
{
[FieldOffset(0)]公共只读MouseInput英里;
[FieldOffset(0)]公共KeyboardInputき;
[FieldOffset(0)]公共只读HardwareInput喜;
}

[StructLayout(LayoutKind.Sequential)]
私人结构MouseInput
{
公共只读INT DX;
公共只读INT DY;
公共只读UINT mouseData;
公共只读UINT dwFlags中;
公共只读UINT时间;
公共只读的IntPtr dwExtraInfo;
}

[StructLayout(LayoutKind.Sequential)]
私人结构KeyboardInput
{
公共USHORT WVK;
公共USHORT wScan;
公共UINT dwFlags中;
公共只读UINT时间;
公众的IntPtr dwExtraInfo;
}

[StructLayout(LayoutKind.Sequential)]
私人结构HardwareInput
{
公共只读UINT uMsg;
公共只读USHORT wParamL;
公共只读USHORT wParamH;
}

现在使用 SendKey(0×14)发送T可在活动窗口(或游戏)



注意:您需要 KeyEventF.Scancode 作为标志或 wScan 属性将被忽略!


I need some help about DirectInput, I'll tell what i am trying to do. I want to do my program sends key combinations to a game when i press only one key. Examp.: I'll press "r" and it will pres "1","3","2","4" keys. I found some codes from here. But they didn't worked exactly.

    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]));
    }

Here is my code and my question: When I'm using Send_Key_Hold only it presses one key in the game and other combination keys not pressed because first key is holded i think. When I'm using Send_Key_Hold and Send_Key_Release together it doesn't press any buttons on game. But on desktop (i mean anyother application not game) it presses the key.

解决方案

I've found this example on internet. Tested it on my own when I tried to do the same thing as you are trying now.

[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;
}

Now use SendKey(0x14) to send T to the active window (or game).

Note: You need KeyEventF.Scancode as your flag or the wScan property will be ignored!

这篇关于C#的DirectInput SendInput不会影响到游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 23:11