Pyautogui don't work in game window

Cas picture Cas · Jul 25, 2017 · Viewed 7.6k times · Source

I'm making some tests using Pyautogui on games. But in those games that change your cursor and fullscreen games, none of the methods work.

I'm trying now on Ragnarok Online.

I tried:

pyautogui.click()
pyautogui.moveTo(x, y, time)
pyautogui.moveRel(x, y)

None of them works when inside the game window. They work fine outside.

Is there a way to make it work? Or another library that I could use?

By the way, win32api.SetCursorPos((x,y)) also doesn't work.

Thanks.

Answer

obgnaw picture obgnaw · Jul 28, 2017

The source code of Pyautogui

def _sendMouseEvent(ev, x, y, dwData=0):
    assert x != None and y != None, 'x and y cannot be set to None'
    width, height = _size()
    convertedX = 65536 * x // width + 1
    convertedY = 65536 * y // height + 1
    ctypes.windll.user32.mouse_event(ev, ctypes.c_long(convertedX), ctypes.c_long(convertedY), dwData, 0)

and it's a win32API which called SendInput internally.

The SendInput function will insert input events into the same queue as a hardware device but the events are marked with a LLMHF_INJECTED flag that can be detected by hooks. To avoid this flag you probably have to write a custom driver.

There are plenty of answer and question about how to simulate keyboard in DirectX game which some say could and some say could not. And, you may try this an answer which say could

but in my opinion,must game use directx interface to communicate with the hardware for speed,then the SendInput only insert events into the message queue.and you want to use SendInput or Mouse_Event.So as the saying,

There's no problem that can't be solved with another level of indirection

let's add a message queue for the game.

How? VMware. It's done.