3

I can't seem to find a specific answer to this question. How do I click coordinates on the screen without moving the cursor? I am working on a project that will automate installations of programs, but would still like control of the mouse to do other tasks while installations are being processed. Any ideas? Some examples would be awesome as well.

martineau
  • 119,623
  • 25
  • 170
  • 301
Double-A
  • 81
  • 1
  • 1
  • 6
  • Using what? Tk(inter), Gtk, Django with HTML5, a JS tie-in, pygame, pyHook, Raw MFC? Also you should explain what you mean by a click event without cursor move (maybe change screen focus or active window?) as that is unclear. – LinkBerest Dec 01 '15 at 04:24
  • I've messed around with win32api, win32con, and pygame. I haven't had any luck finding documentation about what I'm trying to accomplish. – Double-A Dec 01 '15 at 04:27
  • Even tried pyautogui – Double-A Dec 01 '15 at 04:29
  • You would like to simulate a mouse click on the screen without physically needing a mouse? Not sure about python, but AutoHotKey can do this. – OneCricketeer Dec 01 '15 at 04:30
  • 1
    I'm trying to duplicate the ControlClick function like AutoIt has. Where it sends a mouse click command to a given control in an active window (such as clicking "Next" on an installer) without moving the cursor around the screen. – Double-A Dec 01 '15 at 04:31
  • Okay, so you are familiar with AutoIt, so why not use it? Do you need this functionality to be cross platform? – OneCricketeer Dec 01 '15 at 04:35
  • Yes. I was hoping to create a program that is platform independent. – Double-A Dec 01 '15 at 04:36
  • If you need it to deal with mouse click events outside of the program it will have to be platform based (you just have to tap into the [underlying hooks](https://stackoverflow.com/questions/17244317/using-pyhook-to-get-mouse-coordinates-to-play-back-later) to perform these types of operations). – LinkBerest Dec 01 '15 at 04:40
  • Ok so lets say I want this primarily to run on Windows. What would be the best module and function for what I'm trying to accomplish? – Double-A Dec 01 '15 at 04:47
  • Or should I just stick with AutoIt? (unfortunately) – Double-A Dec 01 '15 at 04:48
  • so this might not be exactly what you're looking for but if you want to do a "tap" without moving the mouse in an android emulator you can use something called ADB which means Android Debugging Bridge Not sure how to use it myself, I just know it's possible – Kian May 18 '20 at 20:14

4 Answers4

4

Thank you to those who have tried to help me out. After further research I have found a solution. I have found a way to import AutoIt into Python by using PyAutoIt. There is a ControlClick function that I have been looking for that clicks a control without moving the mouse cursor. Here is an example:

import autoit
import time

autoit.run("notepad.exe")
autoit.win_wait_active("[CLASS:Notepad]", 3)
autoit.control_send("[CLASS:Notepad]", "Edit1", "hello world{!}")
time.sleep(5)
autoit.win_close("[CLASS:Notepad]")
autoit.control_click("[Class:#32770]", "Button2")

Thanks again. This thread can be marked answered and closed :)

Double-A
  • 81
  • 1
  • 1
  • 6
2

I needed something similar, and the solution I found was this:

import win32gui
import win32api
import win32con


def control_click(x, y, handle, button='left'):

    l_param = win32api.MAKELONG(x, y)

    if button == 'left':
        win32gui.PostMessage(handle, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, l_param)
        win32gui.PostMessage(handle, win32con.WM_LBUTTONUP, win32con.MK_LBUTTON, l_param)

    elif button == 'right':
        win32gui.PostMessage(handle, win32con.WM_RBUTTONDOWN, 0, l_param)
        win32gui.PostMessage(handle, win32con.WM_RBUTTONUP, 0, l_param)

The window does not need to be in the foreground, but it cannot be minimized, the handle and coordinates must be from the control, example: in a notepad, the handle and coordinates are for the 'Edit1' control, not for the window, if the application has no controls visible through a spytool, you can use the handle and the window coordinates, example: the pycharm itself.

Midas Cwb
  • 21
  • 1
0
# hwdn = window Id (Nox, or BlueStack), pos=(x, y) relative pos  in window 
def leftClick(hwnd, pos):
    lParam = win32api.MAKELONG(pos[0], pos[1])

    win32gui.SendMessage(hwnd, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, lParam) 
    win32gui.SendMessage(hwnd, win32con.WM_LBUTTONUP, 0, lParam)
    time.sleep(0.1)
-3

If you know the screen coordinates you want to click, you could try:

import pyautogui
x, y = 500, 250 # or whatever
pyautogui.click(x, y)

This will move to mouse pointer to (x, y) and do a left mouse click.

Steve Misuta
  • 1,033
  • 7
  • 7