不要说用keybd_event。不知道他们的虚拟码是多少?或者是没有?Keys.Control = 131072的数值代表什么,如果是扫描码的话那Ctrl的虚拟码该怎么设置。请高手赐教。

解决方案 »

  1.   

    Shift  + 
     
    Ctrl  ^ 
     
    Alt  % 
     
      

  2.   

            private void button1_Click(object sender, EventArgs e)
            {
                textBox1.Focus();
                SendKeys.Send("+a");
            }
      

  3.   

    http://www.cnblogs.com/thunderdanky/articles/846890.html
      

  4.   

    To the 1st floor and the 2nd floor:
    It's the sendinput function in win32 Api, Not the send function in the framework class sendkeys.
      

  5.   


            [DllImport("user32.dll")]
            public static extern UInt32 SendInput(UInt32 nInputs, INPUT[] pInputs, int cbSize);
            [DllImport("kernel32.dll")]
            public static extern int GetTickCount();
            [StructLayout(LayoutKind.Explicit)]
            public struct INPUT
            {
                [FieldOffset(0)]
                public Int32 type;
                [FieldOffset(4)]
                public KEYBDINPUT ki;
                [FieldOffset(4)]
                public MOUSEINPUT mi;
                [FieldOffset(4)]
                public HARDWAREINPUT hi;
            }        [StructLayout(LayoutKind.Sequential)]
            public struct MOUSEINPUT
            {
                public Int32 dx;
                public Int32 dy;
                public Int32 mouseData;
                public Int32 dwFlags;
                public Int32 time;
                public IntPtr dwExtraInfo;
            }
            [StructLayout(LayoutKind.Sequential)]
            public struct KEYBDINPUT
            {
                public Int16 wVk;
                public Int16 wScan;
                public Int32 dwFlags;
                public Int32 time;
                public IntPtr dwExtraInfo;
            }
            [StructLayout(LayoutKind.Sequential)]
            public struct HARDWAREINPUT
            {
                public Int32 uMsg;
                public Int16 wParamL;
                public Int16 wParamH;
            }        public const int INPUT_KEYBOARD = 1;
            public const int KEYEVENTF_KEYUP = 0x0002;        private void button1_Click(object sender, EventArgs e)
            {
                INPUT[] inDown = new INPUT[4];            inDown[0] = new INPUT();
                inDown[1] = new INPUT();
                inDown[2] = new INPUT();
                inDown[3] = new INPUT();            inDown[0].type = inDown[1].type = inDown[2].type = inDown[3].type = INPUT_KEYBOARD;
                inDown[0].ki.wVk = inDown[2].ki.wVk =(int) Keys.LWin;
                inDown[1].ki.wVk = inDown[3].ki.wVk = (int)Keys.R;            inDown[2].ki.dwFlags = inDown[3].ki.dwFlags = KEYEVENTF_KEYUP;            SendInput(4, inDown, Marshal.SizeOf(inDown[0]));
            }
      

  6.   

    还有一个问题,用上面的方法我发现Ctrl、Alt和Win键都不能弹起,弹起的那个怎么设都没用是怎么回事?
      

  7.   

    再问,sendinput发送Alt键的弹起操作不起作用是怎么回事,程序关了那个键还是按下状态,必须手动按一下键盘上的Alt键才行,怎么解决?急~~
      

  8.   

    也就是你只发了按下键的消息,而没发keyup的消息
      

  9.   

    To: ZengHD下面是测试代码        private void button1_Click_1(object sender, EventArgs e)
            {
                INPUT[] inDow = new INPUT[4];
                int i = 0;            while (i < inDow.Length)
                {
                    inDow[i] = new INPUT();
                    inDow[i++].type = INPUT_KEYBOARD;
                }            inDow[0].ki.wVk = inDow[3].ki.wVk = (int)Keys.LMenu;
                inDow[1].ki.wVk = inDow[2].ki.wVk = (int)Keys.E;
                inDow[2].ki.dwFlags = inDow[3].ki.dwFlags = KEYEVENTF_KEYUP;            this.Text = API.SendInput(6,inDow, Marshal.SizeOf(inDow[0])).ToString();        }Form上有个文本框,对窗口定义了一个事件的响应,如果按下Alt+E组合键的话,文本框的Text="menu",这点是成功了,但是关闭程序以后发现Alt一直没弹起,也就是inDow[3]这个发送过去但是没起作用,但是发送成功了,因为Form的Text显示了4。
    怎么测试出没有弹出呢
    关闭程序以后很多鼠标操作都变了,比如双击桌面的程序快捷方式打不开程序,而是打开快捷方式的属性,直到我手动按一下键盘上的LMenu键才恢复正常。
    是什么原因,该如何解决,希望帮助解答,给你加分,谢谢!
      

  10.   

    this.Text = API.SendInput(4,inDow, Marshal.SizeOf(inDow[0])).ToString();
    第一个参数
      

  11.   

    UINT SendInput(
      UINT nInputs,     // count of input events
      LPINPUT pInputs,  // array of input events
      int cbSize        // size of structure
    );第一个参数应该是按键的次数
    我测试过修改为4后就没有问题了睡觉
      

  12.   

    发现问题了,我原来把
            [StructLayout(LayoutKind.Sequential)]
            public struct KEYBDINPUT
            {
                public Int16 wVk;
                public Int16 wScan;
                public Int32 dwFlags;
                public Int32 time;
                public IntPtr dwExtraInfo;
            }
    给定义成下面这样了,浪费了一天时间,郁闷
            [StructLayout(LayoutKind.Sequential)]
            public struct KEYBDINPUT
            {
                public int wVk;
                public int wScan;
                public Int32 dwFlags;
                public Int32 time;
                public IntPtr dwExtraInfo;
            }