How to simulate a mouse click and drag? - python

I want to simulate an event where I left click on the Windows desktop, I drag the mouse a little to create a Selection box to another point, and then keep holding the left button at that point for some time without the Selection box disappearing.
The problem is, I can't get him to keep the Selection box, whenever he gets to the other point the Selection box disappears indicating that the button has been released.
I tried to implement in Python using PyAutoGUI. I tried several ways to do this but still unsuccessfully. Is there any function I'm missing?
import time
import pyautogui
time.sleep(3)
while True:
pyautogui.moveTo(1080, 380)
pyautogui.mouseDown(button='left')
pyautogui.dragTo(917, 564, 1, button='left')
time.sleep(10)
pyautogui.mouseUp(button='left')
time.sleep(2)

Simply removing 2 lines of code and changing dragTo() to moveTo() seems to do what you are trying to do:
import time
import pyautogui
time.sleep(3)
while True:
pyautogui.moveTo(1080, 380)
pyautogui.mouseDown(button='left')
pyautogui.moveTo(917, 564, 1)
time.sleep(10)

This might help you a bit:
pyautogui.moveTo(1277, 127)
pyautogui.dragTo(1277, 225, button='left', duration=5)
(duration is in seconds)

Related

Pyautogui macro background

I have been learning python recently and decided to learn more about pyautogui, what you see down below is a macro of mine. My question is simple; is there a way to let this macro run in a specific window. For example: I want this macro to run in google chrome while I am in discord chatting with my friends (text channel so I'm not in the google chrome window). (Ignore my sloppy method of writing code)
import pyautogui
import random
import time
import mouse
#############################
tijd = 0
actief = 0
float (actief)
#############################
while not mouse.is_pressed('right'):
time.sleep(0.01)
bank_x1, bank_y1 = pyautogui.position()
time.sleep (0.5)
while not mouse.is_pressed('right'):
time.sleep(0.01)
bank_x2, bank_y2 = pyautogui.position()
print ("{} {} {} {}".format(bank_x1,bank_x2,bank_y1,bank_y2))
#############################
lijst = [[bank_x1,bank_x2,bank_y1,bank_y2,200,243],[1203,1236,721,749,23,49],[390,422,112,140,22,46]]
while not mouse.is_pressed('middle') or actief > tijd:
for i in range(0, 4):
x = random.randint(lijst[i][0], lijst[i][1])
y = random.randint(lijst[i][2], lijst[i][3])
pyautogui.moveTo(x, y)
wacht = random.randint(lijst[i][4], lijst[i][5]) / 100
time.sleep(wacht)
str (actief_str)
pyautogui.click()
pyautogui.press('esc')
No. Not with pyautogui.
Pyautogui simulates actual keyboard and mouse input to the system, not to a specific window. Thus, it will always act exactly the same as if you were pressing the keys and clicking the mouse yourself. So, no. You cannot use it to send keystrokes to background applications. The keystrokes will always effect the window that is focused, just as they do when you physically input them with a keyboard or mouse.
This question shows how you could achieve it with winapi, but it is much more complicated and less user-friendly than pyautogui.

How to make the Keyboard module detect if a right click was made

I have tried to search google and other websites such as GitHub but I cant find a way to detect if the right key has been pressed. I am using the two modules Keyboard and Pyautogui to make a auto clicker but all the ideas that I have come up with have failed. Here is my code:
import keyboard
import pyautogui
pyautogui.PAUSE = 0.1
while True:
if keyboard.is_pressed('h'):
pyautogui.rightClick()
if keyboard.is_pressed('g'):
pyautogui.click()
I want a way to replace the h and g with right click and left click any ideas?
If you are trying to check for just mouse clicks, the pynput library could work.
from pynput import mouse
# mouse.Events looks at all events, you could use
# events = mouse.Events().get(1) to look at just the events in the last second
with mouse.Events() as events:
for event in events:
if isinstance(event, mouse.Events.Click):
if event.button == mouse.Button.right and event.pressed:
#Do stuff for right click
else:
print('Received event {}'.format(event))
I am using Click so that movements are not tracked. Similarly, press down/up are counted as separate events, so to filter those out using event.pressed
Take a look at https://pynput.readthedocs.io/en/latest/mouse.html for other mouse listener ideas

Autoclicker does not activate the window

I wanted to create an autoclicker that would "remember" current position of my mouse pointer, move to specific location on the desktop, perform a double click and then come back to where it was and will do this randomly every 1 to 4 seconds. This way I wanted to achieve an autoclick in a specific place and more or less be able to use my mouse to browse other stuff.
What I want to click is in a different window, it is a program that I leave open visible on one half of my desktop and on the other half I want to do other things. The problem is that auto clicker does not make the program an active window and the click does not work.
import pyautogui
import threading
import random
def makro():
z = random.randint(1,4) #timer set to random value between 1 and 4 seconds
(x, y) = pyautogui.position() #remember current position of mouse pointer
threading.Timer(z, makro).start()
pyautogui.doubleClick(1516, 141) #perform a double click in this location (this clicks do not make the window active and clicks do not work)
pyautogui.moveTo(x, y) #come back to original mouse pointer location
makro()
Thank you for help
I think adding
pyautogui.click(1516, 141) before pyautogui.doubleClick(1516, 141) could activate the window.

Run a python code only when a specific object appears in screen

In the following image, I want Python (using module Pyautogui) to click the "OK" button.
The problem is, I dont know when the "OK" button appears. So how do I make python to click the "OK" button only when it appears?
References:
If you wonder it looks like a mobile screen, I am using an android emulator.
PyAutoGUI has a function called locateOnScreen() which takes a screenshot of your current screen and then looks for the provided image you pass into the parameter of the function.
First, you need to have a picture of the "OK" button to tell PyAutoGUI what to look for, which you can do using pyautogui.screenshot('ok.png', region=(0, 0, 300, 400)) the region parameter specifies where to look and the size of the picture.
After that, you can run locateOnScreen('ok.png') and if the picture 'ok.png' is currently on your monitor it will return the center coordinates, which you can then pass to pyautogui.click()
import pyautogui
pyautogui.screenshot('ok.png', region=(0, 0, 300, 400))
Run that line of code and play around with the region parameter until you get a good looking 'ok.png' and make sure only the button is visible in the picture as PyAutoGUI looks for an exact match. Once you're done, replace the line with the following:
import pyautogui
location = pyautogui.locateOnScreen('ok.png')
pyautogui.click(location)
Now, whenever the "OK" button is on your screen, PyAutoGUI will click on it

Python script to control mouse clicks

I created a small Python script using win32api to use on the popular game Cookie Clicker (a game where you have to click on a Big Cookie to gain points) just for fun. It has a function called "auto_clicker" that do just that: keeps clicking on the screen on the point the user defined. This is the script:
# -*- coding: utf-8 -*-
import win32con
import win32api
def clicker(x,y):
"""Clicks on given position x,y
Input:
x -- Horizontal position in pixels, starts from top-left position
y -- Vertical position in pixels, start from top-left position
"""
win32api.SetCursorPos((x,y))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)
def auto_clicker(x = -1,y = -1):
"""Keep clicking on position x,y. If no input is given, gets from actual
mouse position.
"""
if x == -1 | y == -1:
x,y = win32api.GetCursorPos()
while True:
clicker(x,y)
It works nicely, but I want to make some improvements:
How can I get the cursor position only when the user clicks instead when the function is called? I would prefer to not add another module
since win32api seems to contain everything I needed. Tried this
method without success.
How can I detect a keypress like "Escape", so I can exit from my program without the ugly hack I am using now (Ctrl+Alt+Del seems to give SetCursorPos denied access, so Python throws a error and exit the program).
Can I make this program portable? Seems like I can do using Tkinter and generating a invisible Tk window, but I tried to write something without success.
I don't think with win32api you can listen to clicks you can just generate them (not sure though). However, try using pyHook, it's a simple api easy to use and can be found here http://sourceforge.net/apps/mediawiki/pyhook/index.php?title=Main_Page. With pyhook you can create a listener to listen to a mouse event and upon a mouse click you can do whatever you want, the example in the link shows you how. As for key press, you can use the same api for that too, also an example is provided, good luck!
use pynput . It can control mouse, keyboard, etc.
examples:
from pynput.mouse import Button, Controller
mouse = Controller()
# Read pointer position
print('The current pointer position is {0}'.format(
mouse.position))
# Set pointer position
mouse.position = (10, 20)
print('Now we have moved it to {0}'.format(
mouse.position))
# Move pointer relative to current position
mouse.move(5, -5)
# Press and release
mouse.press(Button.left)
mouse.release(Button.left)
# Double click; this is different from pressing and releasing
# twice on Mac OSX
mouse.click(Button.left, 2)
# Scroll two steps down
mouse.scroll(0, 2)

Categories