zoukankan      html  css  js  c++  java
  • 模拟操作(键盘、鼠标)一

       命名空间,引用:

      鼠标:

       Using System.IO;

       Using System.Security.Cryptography;

     [DllImport("user32.dll", EntryPoint = "mouse_event", SetLastError = true)]

       private static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

       private static extern int  mouse_event(int 事件对应值, int X坐标, int Y坐标, int 轮子,int 信息);

    鼠标键位:

        const int MOUSEEVENTF_MOVE = 0x0001;    //  移动鼠标
            const int MOUSEEVENTF_LEFTDOWN = 0x0002;// 模拟鼠标左键按下
            const int MOUSEEVENTF_LEFTUP = 0x0004; //模拟鼠标左键抬起
            const int MOUSEEVENTF_RIGHTDOWN = 0x0008; //模拟鼠标右键按下
            const int MOUSEEVENTF_RIGHTUP = 0x0010;// 模拟鼠标右键抬起
            const int MOUSEEVENTF_MIDDLEDOWN = 0x0020; //模拟鼠标中键按下
            const int MOUSEEVENTF_MIDDLEUP = 0x0040; //模拟鼠标中键抬起
            const int MOUSEEVENTF_ABSOLUTE = 0x8000; //标示是否采用绝对坐标

    简单调用:

         //首先让鼠标移动到固定位置 x=2000,y=2000 左上方开始,向下移动
                mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, 2000, 2000, 0, 0);
                
                //这里执行双击事件(其实双击就是两个单击(鼠标左键))
                mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 2000, 2000, 0, 0);
                mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 2000, 2000, 0, 0);

    键盘操作:

      [DllImport("user32.dll")]

            static extern IntPtr SetActiveWindow(IntPtr hWnd);

            [DllImport("user32.dll")]

            [return: MarshalAs(UnmanagedType.Bool)]

            static extern bool SetForegroundWindow(IntPtr hWnd);

      //基于当前位置操作tab健、Enter健(注意大括号)

           SendKeys.SendWait("{Tab}");

      SendKeys.SendWait("{Enter}");

  • 相关阅读:
    vue store获取值时 Computed property "activeTag" was assigned to but it has no setter.
    深拷贝实现方法以及问题
    mac 中git操作账号的保存与删除
    解决Ubuntu编译内核uImage出现问题“mkimage” command not found
    ubuntu修改主机名后无法解析主机
    多行宏定义中的注释问题
    uboot启动阶段修改启动参数方法及分析
    ubuntu nfs服务器配置
    mount/umount命令详解
    Ubuntu下vsftp安装和配置
  • 原文地址:https://www.cnblogs.com/JeffController/p/4036195.html
Copyright © 2011-2022 走看看