且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-06-15 23:07:07

我在Python中进行自动化测试.我倾向于使用以下内容:

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

http://www.tizmoi.net/watsup/intro.html
链接已失效,存档版本:

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

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

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

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.

您可以通过以下方式发送按键信号:

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

这一切都在Windows中.如果您在其他环境中,我不知道.

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