简体   繁体   中英

SendKeys using pywin32 to a PC Game

I'm using python and pywin32 library. I can perfectly send for example an Enter, or write 'a' in firefox. But I'm trying to do this with a pc game and it's like game wouldn't detect the keypress.

This is my code

import win32com.client as comclt
wsh = comclt.Dispatch("WScript.Shell")
wsh.AppActivate("pc game")
wsh.SendKeys("{ENTER}") #doesn't detect this :S

Most Windows games use the DirectX API for reading keystrokes, they simply ignore that kind of key-pressing messages. They interact directly with the hardware. So your code won't work in the 99% of the Windows games out there.

I would achieve this by writing my own driver and send keystrokes like if they are being pressed manually. It's complex, but not impossible.

Also, I think there's a better way to achieve whatever you want to do. In most cases sending keystrokes it's not a clean solution (specially if you are trying to cheat).

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM