Question

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
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top