'Pyautogui don't work in game window
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.
Solution 1:[1]
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 aLLMHF_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.
Solution 2:[2]
This issue has been addressed using the pydirectinput library: https://learncodebygaming.com/blog/pyautogui-not-working-use-directinput
pip install pydirectinput
Then replace the import in your code:
import pytautogui
import pydirectinput
Replace every function call from pyautogui
to pydirectinput
and you're set.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | MmBaguette |