哪种是在Python上模拟键盘和鼠标的最简单方法?

时间:2022-06-15 05:55:00

I need to do some macros and I wanna know what is the most recommended way to do it.

我需要做一些宏,我想知道最推荐的方法是什么。

So, I need to write somethings and click some places with it and I need to emulate the TAB key to.

所以,我需要编写一些东西并点击一些地方,我需要模拟TAB键。

4 个解决方案

#1


24  

I do automated testing stuff in Python. I tend to use the following:

我用Python自动测试东西。我倾向于使用以下内容:

http://www.tizmoi.net/watsup/intro.html
Edit: Link is dead, archived version: https://web.archive.org/web/20100224025508/http://www.tizmoi.net/watsup/intro.html

http://www.tizmoi.net/watsup/intro.html编辑:链接已死,存档版本:https://web.archive.org/web/20100224025508/http://www.tizmoi.net/watsup/ intro.html

http://www.mayukhbose.com/python/IEC/index.php

I do not always (almost never) simulate key presses and mouse movement. I usually use COM to set values of windows objects and call their .click() methods.

我并不总是(几乎从不)模拟按键和鼠标移动。我通常使用COM来设置windows对象的值并调用它们的.click()方法。

You can send keypress signals with this:

您可以使用以下命令发送按键信号:

import win32com.client

shell = win32com.client.Dispatch("WScript.Shell")
shell.SendKeys("^a") # CTRL+A may "select all" depending on which window's focused
shell.SendKeys("{DELETE}") # Delete selected text?  Depends on context. :P
shell.SendKeys("{TAB}") #Press tab... to change focus or whatever

This is all in Windows. If you're in another environment, I have no clue.

这完全在Windows中。如果你在另一个环境中,我不知道。

#2


15  

Maybe you are looking for Sendkeys?

也许您正在寻找Sendkeys?

SendKeys is a Python module for Windows that can send one or more keystrokes or keystroke combinations to the active window.

SendKeys是一个用于Windows的Python模块,可以将一个或多个击键或击键组合发送到活动窗口。

it seems it is windows only

它似乎只是窗户

Also you have pywinauto (copied from my SO answer)

你有pywinauto(从我的答案复制)

pywinauto is a set of open-source (LGPL) modules for using Python as a GUI automation 'driver' for Windows NT based Operating Systems (NT/W2K/XP).

pywinauto是一组开源(LGPL)模块,用于将Python用作基于Windows NT的操作系统(NT / W2K / XP)的GUI自动化“驱动程序”。

and example from the web page

和网页上的例子

> from pywinauto import application
> app = application.Application.start("notepad.exe")
> app.notepad.TypeKeys("%FX")
> app.Notepad.MenuSelect("File->SaveAs")
> app.SaveAs.ComboBox5.Select("UTF-8")
> app.SaveAs.edit1.SetText("Example-utf8.txt")
> app.SaveAs.Save.Click()

#3


7  

pyautogui is a great package to send keys and automate several keyboard / mouse related tasks. Check out Controlling the Keyboard and Mouse with GUI Automation and PyAutoGUI’s documentation.

pyautogui是一个很棒的包,可以发送密钥并自动执行几个与键盘/鼠标相关的任务。查看使用GUI Automation和PyAutoGUI文档控制键盘和鼠标。

#4


0  

You can use PyAutoGUI library for Python which works on Windows, macOS, and Linux.

您可以将PyAutoGUI库用于适用于Windows,macOS和Linux的Python。

Mouse

Here is a simple code to move the mouse to the middle of the screen:

这是一个将鼠标移动到屏幕中间的简单代码:

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

Docs page: Mouse Control Functions.

文档页面:鼠标控制功能。

Related question: Controlling mouse with Python.

相关问题:使用Python控制鼠标。

Keyboard

Example:

pyautogui.typewrite('Hello world!')                 # prints out "Hello world!" instantly
pyautogui.typewrite('Hello world!', interval=0.25)  # prints out "Hello world!" with a quarter second delay after each character

Docs page: Keyboard Control Functions.

文档页面:键盘控制功能。


More reading: Controlling the Keyboard and Mouse with GUI Automation (Chapter 18 of e-book).

更多阅读:使用GUI Automation控制键盘和鼠标(电子书第18章)。

Related questions:

#1


24  

I do automated testing stuff in Python. I tend to use the following:

我用Python自动测试东西。我倾向于使用以下内容:

http://www.tizmoi.net/watsup/intro.html
Edit: Link is dead, archived version: https://web.archive.org/web/20100224025508/http://www.tizmoi.net/watsup/intro.html

http://www.tizmoi.net/watsup/intro.html编辑:链接已死,存档版本:https://web.archive.org/web/20100224025508/http://www.tizmoi.net/watsup/ intro.html

http://www.mayukhbose.com/python/IEC/index.php

I do not always (almost never) simulate key presses and mouse movement. I usually use COM to set values of windows objects and call their .click() methods.

我并不总是(几乎从不)模拟按键和鼠标移动。我通常使用COM来设置windows对象的值并调用它们的.click()方法。

You can send keypress signals with this:

您可以使用以下命令发送按键信号:

import win32com.client

shell = win32com.client.Dispatch("WScript.Shell")
shell.SendKeys("^a") # CTRL+A may "select all" depending on which window's focused
shell.SendKeys("{DELETE}") # Delete selected text?  Depends on context. :P
shell.SendKeys("{TAB}") #Press tab... to change focus or whatever

This is all in Windows. If you're in another environment, I have no clue.

这完全在Windows中。如果你在另一个环境中,我不知道。

#2


15  

Maybe you are looking for Sendkeys?

也许您正在寻找Sendkeys?

SendKeys is a Python module for Windows that can send one or more keystrokes or keystroke combinations to the active window.

SendKeys是一个用于Windows的Python模块,可以将一个或多个击键或击键组合发送到活动窗口。

it seems it is windows only

它似乎只是窗户

Also you have pywinauto (copied from my SO answer)

你有pywinauto(从我的答案复制)

pywinauto is a set of open-source (LGPL) modules for using Python as a GUI automation 'driver' for Windows NT based Operating Systems (NT/W2K/XP).

pywinauto是一组开源(LGPL)模块,用于将Python用作基于Windows NT的操作系统(NT / W2K / XP)的GUI自动化“驱动程序”。

and example from the web page

和网页上的例子

> from pywinauto import application
> app = application.Application.start("notepad.exe")
> app.notepad.TypeKeys("%FX")
> app.Notepad.MenuSelect("File->SaveAs")
> app.SaveAs.ComboBox5.Select("UTF-8")
> app.SaveAs.edit1.SetText("Example-utf8.txt")
> app.SaveAs.Save.Click()

#3


7  

pyautogui is a great package to send keys and automate several keyboard / mouse related tasks. Check out Controlling the Keyboard and Mouse with GUI Automation and PyAutoGUI’s documentation.

pyautogui是一个很棒的包,可以发送密钥并自动执行几个与键盘/鼠标相关的任务。查看使用GUI Automation和PyAutoGUI文档控制键盘和鼠标。

#4


0  

You can use PyAutoGUI library for Python which works on Windows, macOS, and Linux.

您可以将PyAutoGUI库用于适用于Windows,macOS和Linux的Python。

Mouse

Here is a simple code to move the mouse to the middle of the screen:

这是一个将鼠标移动到屏幕中间的简单代码:

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

Docs page: Mouse Control Functions.

文档页面:鼠标控制功能。

Related question: Controlling mouse with Python.

相关问题:使用Python控制鼠标。

Keyboard

Example:

pyautogui.typewrite('Hello world!')                 # prints out "Hello world!" instantly
pyautogui.typewrite('Hello world!', interval=0.25)  # prints out "Hello world!" with a quarter second delay after each character

Docs page: Keyboard Control Functions.

文档页面:键盘控制功能。


More reading: Controlling the Keyboard and Mouse with GUI Automation (Chapter 18 of e-book).

更多阅读:使用GUI Automation控制键盘和鼠标(电子书第18章)。

Related questions: