Detecting Mouse Clicks in Windows Using Python

Detecting Mouse clicks in windows using python

The only way to detect mouse events outside your program is to install a Windows hook using SetWindowsHookEx. The pyHook module encapsulates the nitty-gritty details. Here's a sample that will print the location of every mouse click:

import pyHook
import pythoncom

def onclick(event):
print event.Position
return True

hm = pyHook.HookManager()
hm.SubscribeMouseAllButtonsDown(onclick)
hm.HookMouse()
pythoncom.PumpMessages()
hm.UnhookMouse()

You can check the example.py script that is installed with the module for more info about the event parameter.

pyHook might be tricky to use in a pure Python script, because it requires an active message pump. From the tutorial:

Any application that wishes to receive
notifications of global input events
must have a Windows message pump. The
easiest way to get one of these is to
use the PumpMessages method in the
Win32 Extensions package for Python.
[...] When run, this program just sits
idle and waits for Windows events. If
you are using a GUI toolkit (e.g.
wxPython), this loop is unnecessary
since the toolkit provides its own.

How to detect if mouse was clicked?

I was able to make it work just with win32api. It works when clicking on any window.

import win32api
import time

width = win32api.GetSystemMetrics(0)
height = win32api.GetSystemMetrics(1)
midWidth = int((width + 1) / 2)
midHeight = int((height + 1) / 2)

state_left = win32api.GetKeyState(0x01) # Left button up = 0 or 1. Button down = -127 or -128
while True:
a = win32api.GetKeyState(0x01)
if a != state_left: # Button state changed
state_left = a
print(a)
if a < 0:
print('Left Button Pressed')
else:
print('Left Button Released')
win32api.SetCursorPos((midWidth, midHeight))
time.sleep(0.001)

Controlling mouse with Python

Tested on WinXP, Python 2.6 (3.x also tested) after installing pywin32 (pywin32-214.win32-py2.6.exe in my case):

import win32api, win32con
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(10,10)

Co-ordinates of Mouse when clicked - Python

Try this solution:

from pynput import mouse

def on_click(x, y, button, pressed):
print(x,y)

with mouse.Listener(on_click=on_click) as listener:
listener.join()

The result of above code is:

830 345 Button.left True
830 345 Button.left False

The values of above code will print twice i.e one for co-ordinate value for button pressed and one co-ordinate value for button released.

So to get both the co-ordinates of mouse click event and released event use the above code.

So to print the value of co-ordinates where mouse button is clicked use the below code:

from pynput import mouse

def on_click(x, y, button, pressed):
if pressed == True:
print(x,y)

with mouse.Listener(on_click=on_click) as listener:
listener.join()

AND

So to print the value of co-ordinates where mouse button is released use the below code:

from pynput import mouse

def on_click(x, y, button, pressed):
if pressed == False:
print(x,y)

with mouse.Listener(on_click=on_click) as listener:
listener.join()

How to detect mouse click in Python 3.6.1

The only way to detect mouse events outside your program is to install a Windows hook using SetWindowsHookEx. The pyHook module encapsulates the nitty-gritty details.

import pyHook
import pythoncom

def onclick(event):
print event.Position
return True

hm = pyHook.HookManager()
hm.SubscribeMouseAllButtonsDown(onclick)
hm.HookMouse()
pythoncom.PumpMessages()
hm.UnhookMouse()

pyHook might be tricky to use in a pure Python script, because it requires an active message pump

How to make python script that performs a Mouse Click in Windows 10?

Try this way:

import pywinauto
app = pywinauto.Application().connect(path='your_process_name.exe')
app.MainDialog.click_input(coords=(x, y))

For click method to work you need to specify the process/dialog on which the coordinate is present.
Use connect() method to connect to a existing method else use start() to open new instance.

Is it possible to detect if the mouse is held down with python

You can use pynput module to do it. It can be installed using pip command pip install pynput. Also, see the documentation to understand the full functionality of pynput. Basically it is used to log key input of keyboard and mouse.

Here is how you can check if the mouse key is held down or not.

from pynput.mouse import Listener

# This function will be called when any key of mouse is pressed
def on_click(*args):
# see what argument is passed.
print(args)
if args[-1]:
# Do something when the mouse key is pressed.
print('The "{}" mouse key has held down'.format(args[-2].name))

elif not args[-1]:
# Do something when the mouse key is released.
print('The "{}" mouse key is released'.format(args[-2].name))

# Open Listener for mouse key presses
with Listener(on_click=on_click) as listener:
# Listen to the mouse key presses
listener.join()


Related Topics



Leave a reply



Submit