views:

2865

answers:

5

How can I detect mouse clicks regardless of the window the mouse is in?

Perferabliy in python, but if someone can explain it in any langauge I might be able to figure it out.

I found this on microsoft's site: http://msdn.microsoft.com/en-us/library/ms645533(VS.85).aspx

But I don't see how I can detect or pick up the notifications listed.

Tried using pygame's pygame.mouse.get_pos() function as follows:

import pygame
pygame.init()
while True:
    print pygame.mouse.get_pos()

This just returns 0,0. I'm not familiar with pygame, is something missing?

In anycase I'd prefer a method without the need to install a 3rd party module. (other than pywin32 http://sourceforge.net/projects/pywin32/ )

A: 

You might want to have a look at this

Feet
Thanks, actually, I tried:-import pygamepygame.init()while True: print pygame.mouse.get_pos()-but just got 0,0 even when moving my mouse.However, it does mention, "The cursor position can be located outside of the display window, but is always constrained to the screen." hmmm
monkut
A: 

The windows way of doing it is to handle the WM_LBUTTONDBLCLK message.

For this to be sent, your window class needs to be created with the CS_DBLCLKS class style.

I'm afraid I don't know how to apply this in Python, but hopefully it might give you some hints.

MrZebra
+1  A: 

Windows MFC, including GUI programming, is accessible with python using the Python for Windows extensions by Mark Hammond. An O'Reilly Book Excerpt from Hammond's and Robinson's book shows how to hook mouse messages, .e.g:

self.HookMessage(self.OnMouseMove,win32con.WM_MOUSEMOVE)

Raw MFC is not easy or obvious, but searching the web for python examples may yield some usable examples.

gimel
+8  A: 

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.

efotinis
This is great! First time I heard about pyhook. Does just what I wanted!
monkut
No idea how I forgot PyHook, but thanks a lot for reminding me :)Also, Nice code sample.
Mikle
+1  A: 

chaosAD on http://www.python-forum.org/pythonforum/viewtopic.php?f=2&t=11154 does it without pyHook.