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}");

  • 相关阅读:
    使用golang访问kubebernetes
    使用 Rancher 管理现有 Kubernetes 集群
    Running powershell scripts during nuget package installation and removal
    How to Create, Use, and Debug .NET application Crash Dumps in 2019
    寻找写代码感觉(一)之使用 Spring Boot 快速搭建项目
    Selenium+Java之解决org.openqa.selenium.InvalidArgumentException: invalid argument报错问题
    Selenium环境搭建
    关于Xpath定位方法知道这些基本够用
    Web自动化之浏览器启动
    【翻译】编写代码注释的最佳实践
  • 原文地址:https://www.cnblogs.com/JeffController/p/4036195.html
Copyright © 2011-2022 走看看