如何用Python控制键盘和鼠标?

发布于 2024-08-15 21:05:51 字数 321 浏览 11 评论 0原文

如何在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

惟欲睡 2024-08-22 21:05:51

我使用dogtail(https://fedorahosted.org/dogtail/)来做这样的事情,用这个我为我的 Linux(Ubuntu) 应用程序创建了一个自动化测试框架。该框架单击按钮并在文本字段中输入内容。

请参阅 gedit 示例, https://fedorahosted.org /dogtail/browser/examples/gedit-test-utf8-procedural-api.py

所以只需使用dogtail例如

dogtail.rawinput.click(100, 100)

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

dogtail.rawinput.click(100, 100)
守望孤独 2024-08-22 21:05:51

我可以建议你 PyAutoGUI,它允许完全控制鼠标和键盘并获取屏幕截图,甚至你可以在屏幕内定位图像(例如:按钮在哪里?),对于动态自动点击非常有用。它适用于 Windows、macOS 和 Linux。

例如:

>>> import pyautogui
>>> screenWidth, screenHeight = pyautogui.size()
>>> pyautogui.moveTo(screenWidth / 2, screenHeight / 2)

查看简介页面

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:

>>> import pyautogui
>>> screenWidth, screenHeight = pyautogui.size()
>>> pyautogui.moveTo(screenWidth / 2, screenHeight / 2)

Check out the Introduction page.

街道布景 2024-08-22 21:05:51

这完全有效……至少在 Mac 上是这样。这是用于单击和拖动等,但可以进行相应的改造。

#!/usr/bin/python
import sys
import time
from Quartz.CoreGraphics import * # imports all of the top-level symbols in the module

def mouseEvent(type, posx, posy):
    theEvent = CGEventCreateMouseEvent(None, type, (posx,posy), kCGMouseButtonLeft)
    CGEventPost(kCGHIDEventTap, theEvent)
def mousemove(posx,posy):
    mouseEvent(kCGEventMouseMoved, posx,posy);
def mouseclickdn(posx,posy):
    mouseEvent(kCGEventLeftMouseDown, posx,posy);
def mouseclickup(posx,posy):
    mouseEvent(kCGEventLeftMouseUp, posx,posy);
def mousedrag(posx,posy):
    mouseEvent(kCGEventLeftMouseDragged, posx,posy);

ourEvent = CGEventCreate(None);
currentpos=CGEventGetLocation(ourEvent); # Save current mouse position
mouseclickdn(60, 100);
mousedrag(60, 300);
mouseclickup(60, 300);
time.sleep(1);
mousemove(int(currentpos.x),int(currentpos.y)); # Restore mouse position

This totally works... on a Mac at least. This is for a click AND drag, etc.. but can be retrofitted accordingly.

#!/usr/bin/python
import sys
import time
from Quartz.CoreGraphics import * # imports all of the top-level symbols in the module

def mouseEvent(type, posx, posy):
    theEvent = CGEventCreateMouseEvent(None, type, (posx,posy), kCGMouseButtonLeft)
    CGEventPost(kCGHIDEventTap, theEvent)
def mousemove(posx,posy):
    mouseEvent(kCGEventMouseMoved, posx,posy);
def mouseclickdn(posx,posy):
    mouseEvent(kCGEventLeftMouseDown, posx,posy);
def mouseclickup(posx,posy):
    mouseEvent(kCGEventLeftMouseUp, posx,posy);
def mousedrag(posx,posy):
    mouseEvent(kCGEventLeftMouseDragged, posx,posy);

ourEvent = CGEventCreate(None);
currentpos=CGEventGetLocation(ourEvent); # Save current mouse position
mouseclickdn(60, 100);
mousedrag(60, 300);
mouseclickup(60, 300);
time.sleep(1);
mousemove(int(currentpos.x),int(currentpos.y)); # Restore mouse position
满地尘埃落定 2024-08-22 21:05:51

这是来自 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.

白况 2024-08-22 21:05:51

对于鼠标,我发现 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...

过度放纵 2024-08-22 21:05:51

对于 Linux,有 Xlib,但它也适用于键盘吗? (仅找到对鼠标的引用)

是的,它也适用于键盘。

For linux there is Xlib but does it works for keyboard as well? (found only reference to the mouse)

Yes, it work for keyboard also.

扮仙女 2024-08-22 21:05:51

对于控制台,请尝试 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.

别想她 2024-08-22 21:05:51

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文