1

我正在尝试编写一个 python 脚本来发送一个按住键信号。现在我所能做的就是:

import win32com.client
shell = win32com.client.Dispatch("Wscript.Shell")
shell.SendKeys("z")

但是,这只会发送一个瞬时按键事件。我想做的是按下键和键,类似于:

shell.SendKeys("z{down}")
time.sleep(.25) 
shell.SendKeys("z{up}")

但我找不到任何有据可查的方法来实现这一点。

编辑:我也尝试了一些类似的东西:

import time
import win32com.client
import win32api
import win32gui
import win32con

time.sleep(2)
shell = win32com.client.Dispatch("Wscript.Shell")
win32api.SendMessage(win32con.HWND_TOP, win32con.WM_CHAR, 90, 0) 
win32api.SendMessage(win32con.HWND_BROADCAST, win32con.WM_KEYDOWN, 90, 1) 
time.sleep(.25)
win32api.SendMessage(win32con.HWND_BROADCAST, win32con.WM_KEYUP, 90, 1) 

整个 HWND 对我来说真的是个谜——从文档中我无法弄清楚如何获取正确的窗口。此外,WM_CHAR 似乎工作,但 WM_KEYDOWN/KEYUP 并没有真正做任何事情。

4

1 回答 1

1

You can use win32api.PostMessage to send WM_KEYDOWN and WM_KEYUP messages. See MSDN for documentation of the parameters. The constants are defined in win32con module.

于 2012-02-14T07:45:20.947 回答