C#操作虚拟键盘和鼠标(及禁用物理鼠标)

虚拟键盘自动输入

         [DllImport("user32.dll")]
        private static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);


        public void WritePassWord(string Str)
        {
            ch = Str.ToCharArray();
            for (int i = 0; i < Str.Length; i++)
            {
                keybd_event((byte)ch[i], 0, 0, 0);
                keybd_event((byte)ch[i], 0, 2, 0);
            }
        }


虚拟鼠标自动输入

        [DllImport("user32.dll")]
        static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo); //UIntPtr指针多句柄类型  


        private static void DoMouseClick(int x, int y)
        {
            int dx = (int)((double)x / Screen.PrimaryScreen.Bounds.Width * 65535); 
            int dy = (int)((double)y / Screen.PrimaryScreen.Bounds.Height * 0xffff); 
            mouse_event(MouseEventFlag.Move | MouseEventFlag.LeftDown | MouseEventFlag.LeftUp | MouseEventFlag.Absolute, dx, dy, 0, new UIntPtr(0)); //点击
        }

        public void ScratchEn()
        {
            DoMouseClick(300, 600);
        }


禁用物理鼠标

[DllImport("user32.dll")]
static extern void BlockInput(bool Block);

BlockInput(true);


你可能感兴趣的:(C#操作虚拟键盘和鼠标(及禁用物理鼠标))