Click on specific area of the Screen using Python - python

Is it possible to make Python click on a specific area of the screen? For example, I want it to move the cursor and click near the upper right corner of the screen on a specific place by itself, maybe with PyAutoGUI.

using pyautogui this can be easily done.
you can get pyautogui using pip install pyautogui in the terminal.
if that didn't work use pip install PyAutoGUI.
than using the code bellow you can click anywhere on the screen
import pyautogui
pyautogui.click(100,200)
#x , y
read more about pyautogui here
Try searching things in googles more often before asking question in stack overflow or other places, this could result in learning python and other thing way faster, for example when I searched your questions title the first thing that came up instantly gave me the answer.

import pyautogui
pyautogui.click(100, 100)
Where x and y coordinates go in .click(x, y)
You can read the docs here

Just to make it easier for use in code.
import pyautogui
def click(a, b):
pyautogui.click(a, b)
click(100, 200)

Related

How can i fix my problem with pyautogui.click?

Need to mention that im new with Python and i decided to create a bot for multiplayer game, to autobuy items on auctionhouse, using opencv and pyautogui, so far everything was going pretty well, the cursor was heading to the right point on the screen (reload auction), but
pyautogui.click(clicks=1) isnt working in game window.
IDE (PyCharm) is running with admin rights, googled alot about the topic, but nothing works so far. Will be pleased if anyone could help me, this is my first big project i really want to work with, so hopefully you guys can help me :D
additional info: game uses Battle Eye anticheat, engine Java (probably... Game is called Stalcraft, you can find it on steam, looks like its something Minecraft-based, but im not sure about it)
OS: Win 10 x64
Python:3.11
What i tried:
pyautogui library (tried pyautogui.MoveTo(x,y) and the method with pyautogui.locateCenterOnScreen("whatever.png",confidence=0.85 Need to mention that first method works only in IDE, the second one based on img recognition also works with browser. Tried this in other apps, but no results. It's just hovering cursor on the right place, but no clicks at all)
pydirectinput library
Here's what i got so far
import cv2
import random
import pyautogui
from time import sleep
import imutils
import numpy as np
import pydirectinput
pyautogui.FAILSAFE=True
rng=random.uniform(0.87, 1.3)
sleep(5)
pyautogui.size()
print(pyautogui.size())
pyautogui.position()
print(pyautogui.position())
pyautogui.moveTo(x=1344, y=342, duration=rng)
pyautogui.click(1344, 342, clicks=5)```
This might be very silly. But I had no luck with .click. In my sample I used .moveTo as you did, but then .mouseDown() and .mouseUp() to simulate a click. Also sometimes it required a delay between the two. I wonder if that combo would help instead?
pyautogui.mouseDown()
sleep(1)
pyautogui.mouseUp()

Clicking on NOT-focused window in python

Well the problem is rather easy to understand. When playing a game, sometimes I need to use autoclicker. So in order to be able to watch some videos on youtube I need to use another monitor & autoclicker.
I wanted to create such autoclicker that would let me send any keyboard/mouse actions to specific window or even process (if it's possible). However I have no idea where to start?
There is a python library called pyautogui you can us it to automate stuff.
Installation
pip install pyautogui
your code should look simething like this
import time
import pyautogui
for ctr in range(10):
pyautogui.click()
time.sleep(1000)
Adjust the time based on requirement and refer this for more info about the library
Better to use power automate to make things simple

Python pyautogui press-function arrows does not work in ribbon

I have a program in Python that starts another executable. Some automated operations need to be done in the ribbon of this open executable, so I use pyautogui to do so.
First the ribbon needs to be ‘active’, so I click on the left most part.
Then I need to use the arrows to change the ribbon menu selection (two times to the left).
Then I need to hit enter to open the correct menu. (going from 'File' to 'Scripting')
The code I’m using for this is:
import pyautogui
pyautogui.click(x=0, y=30)
pyautogui.press(['left', 'left']) #this part does not work here
pyautogui.hotkey('enter')
Somehow, the click and enter do work, but the arrow-keys don’t work. I can use the physical arrow-keys to change the menu selection, but this code doesn’t perform these actions somehow.
Does someone know what is wrong here and how to solve this?
Best regards,
Ganesh
EDIT:
I tried to open both the program and the script with admin right, but that still didn't work. Somehow, the 'enter' and everything else works, except for the arrows.
Ganesh, it might not work with pyautogui as the program/ interface you're using might simply not register the key. To use the arrow keys or other special keys like 'enter' I would suggest using pydirectinput
First, install the library if not already
pip install pydirectinput
Then you can rewrite your code as
import pyautogui
import pydirectinput
pyautogui.click(x=0, y=30)
pydirectinput.press(['left', 'left']) #this is the updated line of code
pyautogui.hotkey('enter')

How do I simulate mouse clicks with Xlib on Python

For educational purposes I've set out to write a python script using cwiid and Xlib so that I can use my wiimote like a mouse.
So far I've gotten the cursor to move by calling disp.warp_pointer(dx,dy) then calling disp.sync() every set time interval. I'm afraid that it might not be the most efficient way to do it but at least for now, it's simple and works well enough.
The problem I'm having more difficulty with is mouse clicks. How do I simulate a mouse click in Xlib? I would like separate press and release events so that I can drag and drop stuff. I've come across this post, but all of the solutions there seem to use other libraries.
using only python Xlib :
from Xlib import X
from Xlib.display import Display
from Xlib.ext.xtest import fake_input
d = Display()
fake_input(d, X.ButtonPress, 1)
d.sync()
fake_input(d, X.ButtonRelease, 1)
d.sync()
The third parameter to fake_input selects the mouse button being simulated. 1/2/3 being left/middle/right buttons, 4/5 and 6/7 should do vertical and horizontal wheel scrolls.
On plain Xlib (C language), you can use the XTestExtension or XSendEvent(). I'm not sure about their python bindings. There are probably bindings for their xcb versions using xpyb.
There's also a binary called xte from the xautomation package (on Debian, sudo apt-get install xautomation and then man xte). xte is very easy to use, and you can also look at its source code to learn how to use the XTestExtension.
Pointers:
http://cgit.freedesktop.org/xorg/lib/libXtst/
http://cgit.freedesktop.org/xcb/xpyb/
http://hoopajoo.net/projects/xautomation.html
http://linux.die.net/man/1/xte

Simulate Mouse Clicks on Python

I'm currently in the process of making my Nintendo Wiimote (Kinda sad actually) to work with my computer as a mouse. I've managed to make the nunchuk's stick control actually move the mouse up and down, left and right on the screen! This was so exciting. Now I'm stuck.
I want to left/right click on things via python when I press A, When I went to do a search, All it came up with was tkinter?
So my question is, What do I call to make python left/right click on the desktop, and if it's possible, maybe provide a snippet?
Thank you for your help!
NOTE: I guess I forgot to mention that this is for Linux.
You can use PyMouse which has now merged with PyUserInput. I installed it via pip:
apt-get install python-pip
pip install pymouse
In some cases it used the cursor and in others it simulated mouse events without the cursor.
from pymouse import PyMouse
m = PyMouse()
m.position() #gets mouse current position coordinates
m.move(x,y)
m.click(x,y) #the third argument "1" represents the mouse button
m.press(x,y) #mouse button press
m.release(x,y) #mouse button release
You can also specify which mouse button you want used. Ex left button:
m.click(x,y,1)
Keep in mind, on Linux it requires Xlib.
The evdev package provides bindings to parts of the input handling subsystem in Linux. It also happens to include a pythonic interface to uinput.
Example of sending a relative motion event and a left mouse click with evdev:
from evdev import UInput, ecodes as e
capabilities = {
e.EV_REL : (e.REL_X, e.REL_Y),
e.EV_KEY : (e.BTN_LEFT, e.BTN_RIGHT),
}
with UInput(capabilities) as ui:
ui.write(e.EV_REL, e.REL_X, 10)
ui.write(e.EV_REL, e.REL_Y, 10)
ui.write(e.EV_KEY, e.BTN_LEFT, 1)
ui.syn()
PyAutoGui works superb.. Thanks to Al Sweigart...
An example of mine...
import pyautogui
pyautogui.FAILSAFE = False
for x in range(555, 899):
pyautogui.moveTo(x, x)
Open your terminal and goto cd /usr/share/pyshared/twisted/protocols/mice
may this __init__.py mouseman.py python script will work for you,check them out.
You can install the PyAutoGUI GUI automation module from PyPI (run pip install pyautogui) and then call the pyautogui.click() to click on a certain X and Y coordinates of the screen:
>>> import pyautogui
>>> pyautogui.click(50, 100)
>>> pyautogui.moveTo(200, 200)
PyAutoGUI works on Windows, Mac, and Linux, and on Python 2 and 3. It also can emulate the keyboard, do mouse drags, take screenshots, and do simple image recognition of the screenshots.
Full docs are at https://pyautogui.readthedocs.org/
I didn't see this mentioned, so here it goes - there is also python-dogtail; see:
Automated GUI testing with Dogtail | Red Hat
Testing/Automation/DogtailTutorial - Ubuntu Wiki
It requires "Enable assistive technologies" in the Gnome Desktop - but can in principle obtain e.g. names of GUI buttons of an application, and allow virtual clicks on them (rather than via x/y coordinates).

Categories

Resources