Simulate Mouse in Python doesn't work (Windows) - python

I am quite the beginner and have the folloing problem: I am trying to automate a mouse movement and some key presses and the code works until I select the program window. (So I can run it and use it for every program I tried except the one I want to use it for). The code still runs but the mouse movement it should do does simply not happen and I don't know why.
I have tried it using win32api, win32con and pyautogui both failed. So is there any possibility to make the mouse movements more like real mouse movements? or does anyone have an idea why it might not work?
def click(x,y):
win32api.SetCursorPos((x,y))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)
click(x, y)
and
pyautogui.click(x, y)

From the comments:
As the target application is running with administrator privileges, a nonprivileged program can't simulate events on it.
To work around that, run your script as administrator.

Related

Hook Mouse clicks and intercept them

I am currently trying to stop mouse clicks while my Script is running and still work with them.
If you are confused about the use case I will elaborate at the end of the question.
Currently I can get clicks like this:
import mouse
import time
def mouseHook(event):
if type(event) == mouse.ButtonEvent:
print(event)
mouse.hook(mouseHook)
while 1:
time.sleep(0.25)
But this still lets the clicks go through, how would I intercept them?
use case: simulate a monitor and while mouse is on that monitor send all movements, clicks and keypresses to MacBook (similar to Synergy, Mouse without Borders or Share Mouse)
The hook used by the application only 'hooks' into the process, which means it gets information from it, but can't insert or modify it's code.
For reference on windows that would use (https://learn.microsoft.com/en-us/previous-versions/windows/desktop/legacy/ms644988(v=vs.85))
The easiest way would be to have your application ignore the mouseclicks.

In pyautogui, it seems to be running in only one window

I am making a bot at the moment using pyautogui. When I run it in pycharm, it clicks on wherever my mouse is after 10 seconds, but if I use it on google, it runs but does not click. here is the code:
import pyautogui
import time
time.sleep(10)
while True:
pyautogui.click()
does anyone know how to fix this (btw i'm using chromebook)

send mouse/keyboard inputs to active/inactive windows

I use python and pynput to automate my mouse.
But obviously, it is impossible to use the computer at the same time for other things. So I'm looking for a solution to either automate a "second" virtual mouse or to just send mouse clicks to a specific window (active or inactive) on Windows 10 without actually using the real mouse.
You can use pyautogui to automate keyboard and mouse actions; but if you are using the keyboard and mouse it will interfere with these commands. the same could be said about adding another mouse; it doesn't add a second OS pointer; it only creates another (at times conflicting) control over that pointer.

Cannot click on game window in Python

I was trying to make a bot for a game, which you can find here. You have to play games to get a tiny amount of bitcoin, like satoshis. Anyway, I as going to make a bot, but when I tried to click on the game window, it didn't do anything. Here is my code:
clicker.py
import pyautogui
while True:
x, y = pyautogui.position()
pyautogui.click(x, y, button='left')
I don't know what's going on, is it my code, or is it the website html? If it is the html, what should i change?
What may be happening is that when you run the program, your mouse clicks so fast that the site does not recognize the clicks. But anyways, how the program is supposed to stop? Your mouse will be clicking everywhere and you may not be able to close the Python window. Do something like this:
import pyautogui
from time import sleep
from threading import Thread
def click_function():
while True:
x, y = pyautogui.position()
pyautogui.click(x, y, button='left')
sleep(0.5) # an acceptable waiting time between one click and another
t = Thread(target=click_function)
t.daemon = True
t.start()
sleep(60)
# Here you put the amount of time you want the program
# to run, in seconds. This will ensure that the program
# stops sometime and you don't get stuck.
You can try adding sleep(n) at the beginning of the code. That is, you run the program, enter the site normally and point your mouse at the specific part where you want to click. Change the value of n to a considerable amount of time in seconds you can perform these tasks. If this doesn't work, you can try the Pynput module, which works similarly to PyAutoGUI.

Create a delay between mouse clicks using Autokey

I am about to switch from Windows to Ubuntu. Since my mouse keeps doing multiple clicks each time I press the middle mouse button, I used AutoHotkey under Windows to add a delay after each click. This worked fine. Now under Ubuntu I want to use AutoKey to do the same. Autokey uses Python for its scripts though.
Here is the AutoHotkey script:
MButton::
If (A_TimeSincePriorHotkey < 200)
Return
Send {MButton}
Return
Currently, (as of version 0.95.4), this is not possible from within AutoKey, because it can’t handle mouse buttons as hotkeys.
This stackoverflow question may be of help: Triggering AutoKey Script via Mouse Button - How To?

Categories