Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 631
Cas Avatar asked Jul 25 '17 06:07

Cas


People also ask

Why is Pyautogui not working?

The Python "ModuleNotFoundError: No module named 'pyautogui'" occurs when we forget to install the pyautogui module before importing it or install it in an incorrect environment. To solve the error, install the module by running the pip install pyautogui command.

What is faster than Pyautogui?

As expected, pyscreeze is slightly faster than pyautogui , but PIL beats them by a factor of 10!

Can Pyautogui be detected?

A kernel-level anti-cheat can detect something like pyautogui running and flag it as a cheat. Cheaters have gone from scripts to custom drivers to custom hardware, so which can be detected depends on how evolved the anti-cheat is.

How do I activate Pyautogui?

hotkey('ctrl', 'c') # Press the Ctrl-C hotkey combination. >>> pyautogui. alert('This is the message to display. ') # Make an alert box appear and pause the program until OK is clicked.


1 Answers

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.

like image 114
obgnaw Avatar answered Oct 13 '22 12:10

obgnaw