使用 pywin32 在 python 中模拟击键 [英] Simulating keystrokes in python using pywin32

查看:30
本文介绍了使用 pywin32 在 python 中模拟击键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在 Windows 7 中模拟 python 3.2 中的击键以发送到 GUI.我在我的电脑上安装了 python win32 模块.顺序是 alt+t+r+name+enter.什么是最好的将这些击键发送到活动窗口的方法?任何示例代码都会有很大帮助.感谢您.(我见过一些名为 sendkeys 的模块,但它可以与 pywin32 一起使用吗?我不允许安装任何其他模块)

I am looking to simulate keystrokes in python 3.2 in windows 7 to be sent to a GUI.I have python win32 module installed on my pc.The order is alt+t+r+name+enter.What would be the best way of sending these keystrokes to the active window?any sample code would be of a great help. Thanking you. (I have seen some module called sendkeys but can that be used with pywin32?i am not allowed to install any other modules)

推荐答案

我知道这可能并不完美.我们有一个使用 python 2.7 的测试应用程序.我们利用 Windows 脚本主机发送密钥.我没有时间将任何东西移植到 python 3,但这可能会让你朝着正确的方向前进.应该很相似.

I know this may not be perfect. We have a testing application using python 2.7. We leverage the windows scripting host to send keys. I have not had time to port anything over to python 3, but this might get you in the right direction. It should be pretty similar.

import win32api
import win32com.client

shell = win32com.client.Dispatch("WScript.Shell")
shell.Run("app")
win32api.Sleep(100)
shell.AppActivate("myApp")
win32api.Sleep(100)
shell.SendKeys("%")
win32api.Sleep(500)
shell.SendKeys("t")
win32api.Sleep(500)
shell.SendKeys("r")
win32api.Sleep(500)
shell.SendKeys("name")
win32api.Sleep(500)
shell.SendKeys("{ENTER}")
win32api.Sleep(2500)

这是 Windows 脚本主机的发送键命令的列表.

Here is a list of the send key commands for Windows Scripting Host.

这里使用 pywin32.它默认安装在 Python 的 ActiveState 版本(我正在使用的版本)中.我不确定,但我不相信它默认安装在 Python 的普通版本中.

This uses pywin32. It is installed by default in the ActiveState version of python (which is the one I am using). I am not sure, but I do not believe, that it is installed by default in the plain vanilla version of python.

这篇关于使用 pywin32 在 python 中模拟击键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆