简体   繁体   English

Python 中的模拟鼠标不起作用(Windows)

[英]Simulate Mouse in Python doesn't work (Windows)

I am quite the beginner and have the folloing problem: I am trying to automate a mouse movement and some key presses and the code works until I select the program window.我是初学者并且有以下问题:我正在尝试使鼠标移动和一些按键自动化,并且代码可以工作,直到我 select 程序 window 为止。 (So I can run it and use it for every program I tried except the one I want to use it for). (所以我可以运行它并将它用于我尝试过的每个程序,除了我想要使用它的程序)。 The code still runs but the mouse movement it should do does simply not happen and I don't know why.代码仍然运行,但它应该做的鼠标移动根本没有发生,我不知道为什么。

I have tried it using win32api, win32con and pyautogui both failed.我使用win32api, win32conpyautogui都失败了。 So is there any possibility to make the mouse movements more like real mouse movements?那么有没有可能让鼠标移动更像真正的鼠标移动呢? or does anyone have an idea why it might not work?或者有谁知道为什么它可能不起作用?

   def click(x,y):
       win32api.SetCursorPos((x,y))
       win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
       win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)
   click(x, y)

and

   pyautogui.click(x, y) 

From the comments:从评论:

As the target application is running with administrator privileges, a nonprivileged program can't simulate events on it.由于目标应用程序以管理员权限运行,因此非特权程序无法在其上模拟事件。

To work around that, run your script as administrator.要解决此问题,请以管理员身份运行您的脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM