Python中的按键 [英] Key Presses in Python
问题描述
是否有可能使系统似乎已按下某个键,例如,我需要多次按下 A 键,这样做很费时间手动,我想写点东西来帮我做,而我所知道的唯一的东西就是Python.
Is it possible to make it appear to a system that a key was pressed, for example I need to make A key be pressed thousands of times, and it is much to time consuming to do it manually, I would like to write something to do it for me, and the only thing I know well enough is Python.
一种更好的表达方式,我需要模拟一个按键,即没有捕获按键.
A better way to put it, I need to emulate a key press, I.E. not capture a key press.
更多信息(根据要求): 我正在运行Windows XP,需要将密钥发送到另一个应用程序.
More Info (as requested): I am running windows XP and need to send the keys to another application.
推荐答案
安装 pywin32 扩展.然后,您可以执行以下操作:
Install the pywin32 extensions. Then you can do the following:
import win32com.client as comclt
wsh= comclt.Dispatch("WScript.Shell")
wsh.AppActivate("Notepad") # select another application
wsh.SendKeys("a") # send the keys you want
搜索WScript.Shell对象的文档(我相信在所有Windows XP安装中默认都安装了该文档).您可以在此处开始.
Search for documentation of the WScript.Shell object (I believe installed by default in all Windows XP installations). You can start here, perhaps.
编辑:发送F11
import win32com.client as comctl
wsh = comctl.Dispatch("WScript.Shell")
# Google Chrome window title
wsh.AppActivate("icanhazip.com")
wsh.SendKeys("{F11}")
这篇关于Python中的按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!