Reputation: 13
I am making a program (just for fun) that plays a sound when you click with your mouse, problem is I don't know how to detect mouse clicks... There are many questions with answers about this topic already but for some reason it just doesn't work for me. Either because they suggest pygame or Tkinter which uses a box so the user would need to click in the box to activate the sound, suggest outdated modules, suggest modules that are for some reason just impossible to get using internet and or pip install or the script just don't work. So what is a currently up to date way (which doesn't require the user to click in a box like pygame etc.) to detect mouse clicks? (btw, I use windows 7)
Upvotes: 1
Views: 1236
Reputation: 48
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
Upvotes: 1