如何用Python控制键盘和鼠标?
如何在Python中控制鼠标和键盘?
这个想法是与 Robot( ) Java 中的类。可以说:将鼠标从这里移到这里,单击那里,写出屏幕上的任何内容。
对于 Windows,有 win32api,但我主要使用 Linux。
对于 Linux,有 Xlib,但它也适用于键盘吗? (只找到了鼠标的参考)
有跨平台的解决方案吗? (Linux、Windows 甚至 OS X 都很棒。)
How can I control the mouse and keyboard in Python?
The idea is to do the same as the Robot() class in Java. Be able to say: move the mouse from here to here, click there, write that whatever is on the screen.
For Windows there is win32api but I'm using mainly Linux.
For Linux there is Xlib but does it works for keyboard as well? (found only reference to the mouse)
Is there a cross-platform solution? (Linux, Windows and even OS X would be the great.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我使用dogtail(https://fedorahosted.org/dogtail/)来做这样的事情,用这个我为我的 Linux(Ubuntu) 应用程序创建了一个自动化测试框架。该框架单击按钮并在文本字段中输入内容。
请参阅 gedit 示例, https://fedorahosted.org /dogtail/browser/examples/gedit-test-utf8-procedural-api.py
所以只需使用dogtail例如
I use dogtail (https://fedorahosted.org/dogtail/) to do such things, using this I have created an automated testing framework for my Linux(Ubuntu) app. That framework clicks buttons and types into text fields.
see the gedit example, https://fedorahosted.org/dogtail/browser/examples/gedit-test-utf8-procedural-api.py
So just use dogtail e.g
我可以建议你 PyAutoGUI,它允许完全控制鼠标和键盘并获取屏幕截图,甚至你可以在屏幕内定位图像(例如:按钮在哪里?),对于动态自动点击非常有用。它适用于 Windows、macOS 和 Linux。
例如:
查看简介页面。
I can advise you PyAutoGUI, it allows to full control Mouse and Keyboard and get Screenshots and even you can locate images within the screen (like: where is the button?), very useful to automate clicks dynamically. It works for Windows, macOS, and Linux.
For example:
Check out the Introduction page.
这完全有效……至少在 Mac 上是这样。这是用于单击和拖动等,但可以进行相应的改造。
This totally works... on a Mac at least. This is for a click AND drag, etc.. but can be retrofitted accordingly.
这是来自 Python 论坛的一个有趣的主题:
Python 论坛
编辑:
stackoverflow 上还有一个关于鼠标控制的有趣问题......也许这是一个很好的起点......
使用 Python 进行鼠标控制
答案之一是指一个 Linux 示例......它引导您到一个不错的博客条目。
Here is an interessting Thread from Python Forum for you:
Python Forum
Edit:
There was also an interessting question on stackoverflow regarding mouse control...maybe it is a good starting point..
Mouse Control with Python
One of the Answers is refering to an Linux example...which heads you to an nice blog entry.
对于鼠标,我发现 pymouse 似乎可以工作(我还没有完全尝试了一下,点击键盘时需要一个小技巧(参见问题)
,我不确定 Xlib 是否可以完成这项工作。我仍在寻找如何编写一些内容,但您可以按照此处的解释捕获关键事件< /a> 或在 C 此处 使用 Xlib (但我不知道C)。
这里是一个仅适用于 gnome 的示例(不适用于还足够好)
在 pymouse 中,他们有一个很好的方法让它在 3 个不同的平台上工作,但需要编写 3 个代码......
for the mouse, I've found pymouse which seems to work (I haven't fully tried it, a small hack needed for the click, cf the issues)
for the keyboard, I'm not sure Xlib can do the job. I'm still looking on how to write something but you can catch key event as explained here or in C here using Xlib (but I don't know C).
here is an example working on gnome only (not good enough yet)
In pymouse, they have a nice way to make it work on the 3 different platform but needs to make 3 code...
是的,它也适用于键盘。
Yes, it work for keyboard also.
对于控制台,请尝试 ncurses 或 slang。在其他情况下尝试 PyQt、PyGtk、TkInter。
所有该解决方案都是跨平台的,几乎可以在任何地方工作。
For console try ncurses or slang. In other situation try PyQt, PyGtk, TkInter.
ALL of this solution ARE cross-platform and work almost anywhere.
autopy 是 Linux、Windows 和 Mac 上的跨平台解决方案。
https://github.com/msanders/autopy/
它允许控制鼠标和键盘,截图,并在较大的位图上查找小位图,如果您想自动化某些您无法控制的 GUI 应用程序,这应该非常方便。
A cross platform solution on linux, windows and mac is autopy.
https://github.com/msanders/autopy/
It allows controlling mouse and keyboard, taking screenshots, and finding small bitmaps on larger bitmaps and should be very convenient if you want to automate some gui application you have no control on.