Double-A
Double-A

Reputation: 81

Mouse-click without moving cursor?

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.

Upvotes: 3

Views: 20201

Answers (4)

Midas Cwb
Midas Cwb

Reputation: 21

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.

Upvotes: 2

# 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)

Upvotes: 0

Double-A
Double-A

Reputation: 81

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 :)

Upvotes: 4

Steve Misuta
Steve Misuta

Reputation: 1033

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.

Upvotes: -3

Related Questions