问题
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