'Pyautogui not properly moving the mouse in Roblox

I'm writing code in python to help me accomplish something in a Roblox game. I won't go into too much detail, but basically, the objective is to automatically click at certain points in the screen. To try to accomplish this, I am using pyautogui.click(). So for instance if the point I needed to click at was (300, 500), I'd do pyautogui.click(300, 500)

Outside of Roblox, pyautogui works just fine. However, whenever I have Roblox open and I use the commands, it doesn't work properly. So, let's say the mouse starts out at (0, 0), and I activate a click at (300, 500). When that happens, the mouse cursor doesn't move, and the click happens at (0, 0) where the mouse originally was. However, as soon as I move my actual mouse even slightly, the mouse cursor teleports back to (300, 500) where it was supposed to be.

This makes it impossible to do what I want because I want the program to click at certain spots without me having to move my actual, physical mouse. Does anyone know how I can use python to actually move the mouse properly?



Solution 1:[1]

You have to use win32api and win32con for moving and clicking the mouse, as pyautogui is sometimes slow, so I am using win32api instead.

This is the code I used.

from pyautogui import *
import pyautogui
import time
import win32api, win32con

def click(x,y):
    win32api.SetCursorPos((x,y)) 
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0)
    time.sleep(0.1) #uses time api, to simulate normal input.
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0)

Solution 2:[2]

you have to use pyautoit to make it work inside of roblox, i used autoit.mouse_click("left",x, y) to make the mouse go to an x and y position in your screen and click the left or right click.

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 RedRoyalR
Solution 2 Nxt