c#将关键笔划发送到directx应用程序

时间:2015-01-18 22:08:39

标签: c#

我正在开发一个小型应用程序,它将3“返回键”发送到directx应用程序。但我有一个错误,因为在directx应用程序中只有第一个“返回键”工作..我在记事本中测试过它工作正常..我不知道为什么..

请帮帮我。非常感谢! :)

        private int Itournament;
        private int FLAG = 0x0008;

        [DllImport("user32.dll")]
        static extern UInt32 SendInput(UInt32 nInputs, [MarshalAs(UnmanagedType.LPArray, SizeConst = 1)] INPUT[] pInputs, Int32 cbSize);

        [StructLayout(LayoutKind.Explicit)]
        struct INPUT
        {
            [FieldOffset(0)]
            public int type;
            [FieldOffset(4)]
            public MOUSEINPUT mi;
            [FieldOffset(4)]
            public KEYBDINPUT ki;
            [FieldOffset(4)]
            public HARDWAREINPUT hi;
        }

        [StructLayout(LayoutKind.Sequential)]
        struct MOUSEINPUT
        {
            public int dx;
            public int dy;
            public int mouseData;
            public int dwFlags;
            public int time;
            public IntPtr dwExtraInfo;
        }

        [StructLayout(LayoutKind.Sequential)]
        struct KEYBDINPUT
        {
            public short wVk;
            public short wScan;
            public int dwFlags;
            public int time;
            public IntPtr dwExtraInfo;
        }

        [StructLayout(LayoutKind.Sequential)]
        struct HARDWAREINPUT
        {
            public int uMsg;
            public short wParamL;
            public short wParamH;
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Itournament = Convert.ToInt32(textBox1.Text);
            panel1.Enabled = false;
            button2.Enabled = true;
            Droid(Itournament);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Application.Restart();
        }

        private void Droid(int Itournament_)
        {
            Thread.Sleep(5000);
            Send_Key(0x1C);
            Thread.Sleep(5000);
            Send_Key(0x1C);            

            /*do
            {
                Send_Key(0x14);
                Itournament_--;
            }
            while (Itournament_ != 0);*/
        }

        private void Send_Key(short Keycode)
        {
            INPUT[] InputData = new INPUT[1];

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

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

1 个答案:

答案 0 :(得分:0)

FLAG设置为ScanCode。其他一切看起来都很正确。这是我的枚举。

[Flags]
public enum KeyFlag
{       
    KeyDown = 0x0000,
    ExtendedKey = 0x0001,
    KeyUp = 0x0002,
    UniCode = 0x0004,
    ScanCode = 0x0008
}

尝试使用ScanCode作为FLAG的值发送KeyDown(0x0000)。

Ex:KeyFlag.KeyDown | KeyFlag.ScanCode

请记住使用KeyFlag.KeyUp发送另一个SendInput KeyFlag.ScanCode用于模拟按键并释放向下键。