如何使用pywin32模拟按住 [英] How to emulate press and hold with pywin32

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

问题描述

我正在尝试编写python脚本来发送按住键的信号.现在,我要做的只是以下操作:

I am trying to write a python script to send a press and hold key signal. Right now all I have managed to do is the following:

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

但是,这仅发送瞬时按键事件.我想做的就是按下并按下,类似以下内容:

However, this only sends an instantaneous key pressed event. What I would like to do is a key down and key up, something along the lines of:

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

但是我找不到任何记录的方式来实现这一目标.

But I cannot find any documented way to achieve this.

我也尝试了类似的方法:

I also tried something along the lines of this:

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并没有真正做任何事情.

The whole HWND thing is really a mystery to me - from the documentation I can't figure out how the hell to grab the correct window. Also, WM_CHAR seems to work, but WM_KEYDOWN/KEYUP hasn't really done anything.

推荐答案

您可以使用win32api.PostMessage发送WM_KEYDOWNWM_KEYUP消息.有关参数的文档,请参见MSDN.常量在win32con模块中定义.

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.

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

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