How to simulate a HOLD keydown event using pywin32?

六眼飞鱼酱① 提交于 2020-01-06 06:43:34

问题


I am trying to get this piece of python code to work on windows:

import win32process, win32con, win32gui, win32api, time

HWND = win32gui.GetActiveWindow()
win32api.PostMessage(HWND, win32con.WM_KEYDOWN, win32con.VK_SPACE, 0)
time.sleep(6)
win32api.PostMessage(HWND, win32con.WM_KEYUP, win32con.VK_SPACE, 0)

It is supposed to simulate someone holding down the space key, but it does not work and does not provide any sort of error message. I believe I may be doing something wrong with HWND, but I am not sure if that is the case.. I would like the event to be sent to any active window that I select to choose.

Can you help?


Solved my problem by using ctypes and the user32.keybd_event method.

ctypes.windll.user32.keybd_event(hexx[key], 0, 0, 0)      #Key is down
ctypes.windll.user32.keybd_event(hexx[key], 0, 0x0002, 0) #Key is up

来源:https://stackoverflow.com/questions/13102875/how-to-simulate-a-hold-keydown-event-using-pywin32

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!