Pyautogui not properly moving the mouse in Roblox - python

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?

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)

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.

Yes I've been getting the same problem so I switched up to AHK(Auto Hotkey). I think (Idk what's happening in the background) the win32 api calls made by python is not using a deep method as you have to download and give permission to AHK to achieve that. So Roblox might have found a way to detect physical input tho they still can't detect deep api calls made by AHK

Related

Pyautogui clicks the button but there is no action?

Hi all so basically I’m new to python and using pyautogui
I have been able to do basic operations like moving the mouse to the specified coordinates
The problem
Pyautogui clicks on the button which is (Start learning Java now >>) on
w3 school website.
I want it to click and move to the next page but apparently, it clicks but there is no action.
in the ideal situation, it should click the button and move on to the next page but it doesn't do it although I see the click
Since I'm new to Python in general coming from a non-coding background.
I would love to have some opinion on this and a possible workaround
My operating system is Windows and my laptop is a Lenovo yoga s730
Below is the script/code Im using:
import pyautogui
import webbrowser
import time
webbrowser.open("https://www.w3schools.com/java/")
pyautogui.tripleClick(x = 700, y= 783)
Thanks
I guess you're trying to perform the click before the page has fully loaded, your code is quite correct, try this I hope it helps:
import pyautogui
import webbrowser
import time
webbrowser.open("https://www.w3schools.com/java/")
time.sleep(5) # wait for the page to load
pyautogui.click(700, 783)
if you would like to use some clever workaround the loading time use something like the concept refered to in this answer https://stackoverflow.com/a/56672841/13770671

I am trying to move my camera in minecraft with python

I have successfully found out how to move, drag, and click in Minecraft with python code. The only thing is that I have gotten the cursor to work in the inventory/crafting menu. One line I used to go up was
pyautogui.moveRel(0, -33, duration=0.1)
For some reason, this does not work outside the crafting menu. I am not able to move the camera in the open-world part of Minecraft. I had a problem with the keyboard as well, it would type the keys I would want it to in a text box but not in the real game. I fixed this by using keyboard.press() and keyboard.release() instead of using pyautogui. Pyautogui works for clicking and moving in the crafting menu but not in the real game.
Any thoughts on how to move the camera in-game?
Pyautogui does not work on video games on Windows or any other common OS. I suggest trying https://pypi.org/project/mouse/, as the keyboard function worked for other people.
Pyhon DirectInput Mouse Relative Moving act not as expected
Looking at this, I found the answer. I was trying to move the mouse where the game has a specific directinput I needed to use. I just called MouseMoveTo() with the desired numbers.
For some reason it only works like this MouseMoveTo(0,100,) with the added comma at the end
I got it to work by turning off Raw Input in the Mouse Settings. Which are in Options > Controls > Mouse Settings > Raw Input

How to change driver level keyboard interaction?(using Python or C++)

I am beginner level in programming(less than a year of experience). I am trying to use https://github.com/Sentdex/pygta5/blob/master/directkeys.py to imitate keyboard inputs in one of RPGs. I successfully managed to implement simple movements(WASD) but when I try to use game specific command as example "choose a closest enemy(Using TAB)" game doesn't react to the input. By doing some research I realized that the game most likely have internal driver level keyboard interaction which cannot be activated using Direct Inputs. I am asking for possible ways to solve the issue.
I tried to use virtual keys to try to imitate the keyboard inputs. Also, I tried to reassign one of the movement keys to "chose closest enemy button" but it produces no inputs as well. I am might start extensive research towards even lower-level keyboard interactions and see what I can do from there.
Here is small code sample
from directkeys import KeyPress, PressKey, ReleaseKey, W, ESC, C, TAB
import time
import win32gui
# open a window
handle = win32gui.FindWindow(0, "Game window")
win32gui.SetForegroundWindow(handle)
# Press and Release key
PressKey(W)
time.sleep(3)
ReleaseKey(W)
print("Script finished")

Pyautogui mouse clicks without actually moving the mouse

I'd like to know if it's possible to automate clicks with pyautogui without compromising the funcionality of my cursor. I automate clicks with pyautogui but my cursor becomes useless while the script is running as the cursor moves around the screen. I was wondering if it is possible to either 1) have two cursos and have pyautogui automating one while I operate the other myself, or 2) have pyautogui click on the screen without actually moving my cursor.
I'm guessing the OS (like most of them) does not have support for multiple mouse pointers. This means that pyautogui does not have it either. The closest you can get to the behavior you are describing is to save your current mouse position with pyautogui.position(), then pressing where you want to press and then jumping back to that position. When done quickly you will have control of your mouse pointer between the automated clicks.
Example:
# Save mouse position
(x, y) = pyautogui.position()
# Your automated click
pyautogui.click(200, 300)
# Move back to where the mouse was before click
pyautogui.moveTo(x, y)
PyAutoGui to my knowledge does not support this functionality, however, at least according to this thread here, using autoit with the function ControlClick allows simulated mouse clicks without any associated cursor movement.
first solution is to save the current mouse location then return to it after mouse click as follows
import pyautogui
(x,y)=pyautogui.position()
pyautogui.click(600,300)
pyautogui.moveTo(x,y)
second solution is to make a keyboard shortcut for the button then simply pressing the button without moving the mouse

Python 3 - Is there a way to keep the mouse inside the tkinter window

Python 3
Tkinter
Hey, I'm looking for something that don't allow the user to leave a window if a variable is true/false.
basicly, somethings that keeps the mouse inside the tkinter window
Not directly, no. However you could monitor the mouse position with the <Motion> event and correct it every time it goes outside. How to correct it would depend on your OS; look into pyautogui for a crossplatform solution.
You can't force the mouse to stay in the window, though you can "grab" all of the mouse events, preventing the user from clicking anywhere but on your app. This is very dangerous as you can lock up your computer if your code has a bug in it.
See How can I make area outside toplevel unclickable?

Categories