我是 Python 新手,正在尝试将击键组合发送到我在 Windows 7 下打开的应用程序。虽然我的脚本在我调用 Notepad.exe 进行测试时可以完美运行,但它不适用于我正在尝试的实际应用程序使用。
到目前为止,这是我的代码:
import win32com.client
import time
import SendKeys
import os
from ctypes import *
shell = win32com.client.Dispatch("WScript.Shell")
os.startfile("C:\...exe")
time.sleep( 5 )
shell.SendKeys('%{F4}') # 'Alt+F4' to close the application again.
由于某种原因,应用程序不会响应脚本而关闭。当我在键盘上按“Alt + F4”时,它会按预期关闭。关于这里可能发生的事情的任何想法?
欢迎任何帮助!请记住,我是 Python 新手 ;-)
PS:我已经通过包含以下代码片段验证了应用程序在活动窗口中运行:
import win32ui
wnd = win32ui.GetForegroundWindow()
print wnd.GetWindowText()