How to simulate keys in game with pywinauto
We have had a long journey my past self. Though we have much to learn here is what we discovered:
Sending Virtual Keys will be ignored if the game is running on DirectX.
Use sendinput and send the scan_codes instead.
# http://www.gamespp.com/directx/directInputKeyboardScanCodes.html
import ctypes
import time
SendInput = ctypes.windll.user32.SendInput
W = 0x11
A = 0x1E
S = 0x1F
D = 0x20
Z = 0x2C
UP = 0xC8
DOWN = 0xD0
LEFT = 0xCB
RIGHT = 0xCD
ENTER = 0x1C
# C struct redefinitions
PUL = ctypes.POINTER(ctypes.c_ulong)
class KeyBdInput(ctypes.Structure):
_fields_ = [("wVk", ctypes.c_ushort),
("wScan", ctypes.c_ushort),
("dwFlags", ctypes.c_ulong),
("time", ctypes.c_ulong),
("dwExtraInfo", PUL)]
class HardwareInput(ctypes.Structure):
_fields_ = [("uMsg", ctypes.c_ulong),
("wParamL", ctypes.c_short),
("wParamH", ctypes.c_ushort)]
class MouseInput(ctypes.Structure):
_fields_ = [("dx", ctypes.c_long),
("dy", ctypes.c_long),
("mouseData", ctypes.c_ulong),
("dwFlags", ctypes.c_ulong),
("time",ctypes.c_ulong),
("dwExtraInfo", PUL)]
class Input_I(ctypes.Union):
_fields_ = [("ki", KeyBdInput),
("mi", MouseInput),
("hi", HardwareInput)]
class Input(ctypes.Structure):
_fields_ = [("type", ctypes.c_ulong),
("ii", Input_I)]
# Actuals Functions
def pressKey(hexKeyCode):
extra = ctypes.c_ulong(0)
ii_ = Input_I()
ii_.ki = KeyBdInput( 0, hexKeyCode, 0x0008, 0, ctypes.pointer(extra) )
x = Input( ctypes.c_ulong(1), ii_ )
ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))
def releaseKey(hexKeyCode):
extra = ctypes.c_ulong(0)
ii_ = Input_I()
ii_.ki = KeyBdInput( 0, hexKeyCode, 0x0008 | 0x0002, 0,
ctypes.pointer(extra) )
x = Input( ctypes.c_ulong(1), ii_ )
ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))
if __name__ == '__main__':
pressKey(0x11)
time.sleep(1)
releaseKey(0x11)
time.sleep(1)
Source:
Simulate Python keypresses for controlling a game
http://www.gamespp.com/directx/directInputKeyboardScanCodes.html
Thank you Sentdex for awesome tutorials on Machine Learning.
I've been wanting to do this to some of my favorite games but couldn't get the keys across (due to DirectX).
Next step:
Windows Driver Kit to emulate keypresses...
Python Mouse Click For Game (Direct Input)
I did a lot of research and found pyautoit module. It's really great and easy to use. I think it's only works with 32 bit python i couldn't install it to 64 bit version. You should install autoitx3 too but i'm not sure maybe it can work with just one dll. After install autoitx3 and pyautoit module you can use this sample code :
import autoit
import time
time.sleep(2)
#"left" stand for left click, 1243 and 1035 is x and y coordinates and 1 is number of clicks.
autoit.mouse_click("left", 1243, 1035, 1)
Which is the easiest way to simulate keyboard and mouse on Python?
I do automated testing stuff in Python. I tend to use the following:
http://www.tizmoi.net/watsup/intro.html
Edit: Link is dead, archived version: https://web.archive.org/web/20100224025508/http://www.tizmoi.net/watsup/intro.html
http://www.mayukhbose.com/python/IEC/index.php
I do not always (almost never) simulate key presses and mouse movement. I usually use COM to set values of windows objects and call their .click() methods.
You can send keypress signals with this:
import win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
shell.SendKeys("^a") # CTRL+A may "select all" depending on which window's focused
shell.SendKeys("{DELETE}") # Delete selected text? Depends on context. :P
shell.SendKeys("{TAB}") #Press tab... to change focus or whatever
This is all in Windows. If you're in another environment, I have no clue.
Related Topics
Reference Requirements.Txt for the Install_Requires Kwarg in Setuptools Setup.Py File
Cannot Use Geometry Manager Pack Inside
How to Install a Package Inside Virtualenv
Minimum Euclidean Distance Between Points in Two Different Numpy Arrays, Not Within
Possibilities for Python Classes Organized Across Files
How to Merge Two Lists into a Single List
Python Webdriver to Handle Pop Up Browser Windows Which Is Not an Alert
Convert Spreadsheet Number to Column Letter
How to Get a Single Result from a SQL Query in Python
Can Multiprocessing Process Class Be Run from Idle
Does Python's Time.Time() Return the Local or Utc Timestamp
How to Break a Line of Chained Methods in Python
Iso to Datetime Object: 'Z' Is a Bad Directive