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

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/lxw945655506/article/details/78170759

虚拟键盘自动输入

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


展开阅读全文

没有更多推荐了,返回首页