麒麟桌面系统自动化方案 pyautogui+pythonnet

管理员

麒麟系统模拟鼠标的点击、滑动等操作,键盘输入等操作

pyautogui的安装见上篇文章

可以通过pythonnet封装python脚本,实现点击和输入等操作做到自动化

鼠标移动

        void DoMoveTest()
        {
            try
            {
                if (!PythonEngine.IsInitialized)
                {
                    if (OperatingSystem.IsWindows())
                    {
                        Runtime.PythonDLL = @"python310.dll";
                    }
                    else
                    {
                        Runtime.PythonDLL = @"libpython3.8.so";
                    }
                    PythonEngine.Initialize();
                }
                using (Py.GIL())
                {
                    dynamic pyautogui = Py.Import("pyautogui");
                    pyautogui.moveTo(Convert.ToInt32(X), Convert.ToInt32(Y), 0.5);
                }
               
            }
            catch (Exception ex)
            {
                LogHelper.logHelper.ErrorLog(ex.Message);
            }
        }

键盘输入等

  void DoInputTest()
        {
            try
            {
                if (!PythonEngine.IsInitialized)
                {
                    if (OperatingSystem.IsWindows())
                    {
                        Runtime.PythonDLL = @"python310.dll";
                    }
                    else
                    {
                        Runtime.PythonDLL = @"libpython3.8.so";
                    }
                    PythonEngine.Initialize();
                }
                using (Py.GIL())
                {
                    dynamic pyautogui = Py.Import("pyautogui");
                    pyautogui.moveTo(Convert.ToInt32(X), Convert.ToInt32(Y), 0.5);
                    pyautogui.click();
                    pyautogui.moveRel(100, -590, 0.5);
                    pyautogui.click();
                    dynamic pyperclip = Py.Import("pyperclip");
                    pyperclip.copy("音乐");
                    pyautogui.hotkey("ctrl", 'v');
                }
            }
            catch (Exception ex)
            {
                LogHelper.logHelper.ErrorLog(ex.Message);
            }
        }

以上代码仅用于测试