2008-10-03 401 views
16

无论鼠标在哪个窗口中,我如何检测鼠标点击?使用python检测窗口中的鼠标点击

python perferabliy,但如果有人可以用任何语言解释它,我可能会弄清楚它。

我发现这对微软的网站: http://msdn.microsoft.com/en-us/library/ms645533(VS.85).aspx

但我不看我怎么可以检测或拿起列出的通知。

使用pygame的的pygame.mouse.get_pos()函数如下尝试:

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

这只是返回0,0。 我对pygame不熟悉,是否缺少某些东西?

在任何情况下,我宁愿没有需要安装第三方模块的方法。 (pywin32除外http://sourceforge.net/projects/pywin32/

+0

您正在使用哪个UI工具包/库? – 2008-10-03 03:13:34

+2

我认为这应该可以使用win32ui和ctypes。我发现我可以通过windll.user32.GetCursorPos(pointer(pt_struct))获取鼠标位置(详情请见:http://monkut.webfactional.com/blog/archive/2008/10/2/python-win-mouse - 位) – monkut 2008-10-03 07:44:51

回答

29

在程序外部检测鼠标事件的唯一方法是使用SetWindowsHookEx安装Windows挂钩。 pyHook模块封装了细节的细节。以下是将打印每次鼠标点击的位置的样本:

import pyHook 
import pythoncom 

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

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

您可以检查example.py脚本安装与模块有关事件参数的详细信息。

pyHook在纯Python脚本中使用可能会很棘手,因为它需要一个活动的消息泵。从tutorial

是希望收到全球输入事件 的 通知,必须有一个Windows消息泵的任何应用程序。 获取其中一种方法的最简单方法是使用 Win32 Extensions包中的PumpMessages方法为 。 [...]在运行时,该程序仅处于空闲状态,并等待Windows事件 。如果 您正在使用GUI工具包(例如, wxPython),则此循环不是必需的,因为该工具包提供了它自己的循环。

2

窗口的做法是处理WM_LBUTTONDBLCLK消息。

要发送此类窗口,需要使用CS_DBLCLKS类样式创建窗口类。

恐怕我不知道如何在Python中应用它,但希望它能给你一些提示。

4

可以使用Mark Hammond的Python for Windows extensions通过python访问Windows MFC,包括GUI编程。 An O'Reilly Book Excerpt从哈蒙德和罗宾逊的book展示了如何挂钩鼠标消息,.e.g:

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

原料MFC是不容易的或明显的,但寻找Python示例网站可能会产生一些可用的例子。

10

我使用win32api。它可以在任何窗口上点击。

# Code to check if left or right mouse buttons were pressed 
import win32api 
import time 

state_left = win32api.GetKeyState(0x01) # Left button down = 0 or 1. Button up = -127 or -128 
state_right = win32api.GetKeyState(0x02) # Right button down = 0 or 1. Button up = -127 or -128 

while True: 
    a = win32api.GetKeyState(0x01) 
    b = win32api.GetKeyState(0x02) 

    if a != state_left: # Button state changed 
     state_left = a 
     print(a) 
     if a < 0: 
      print('Left Button Pressed') 
     else: 
      print('Left Button Released') 

    if b != state_right: # Button state changed 
     state_right = b 
     print(b) 
     if b < 0: 
      print('Right Button Pressed') 
     else: 
      print('Right Button Released') 
    time.sleep(0.001) 
1

这是一个炎热分钟,因为这个问题被问,但我想我会分享我的解决方案:我只是用内置模块​​。 (我正在使用Python 3.3 btw)

import ctypes 
import time 

def DetectClick(button, watchtime = 5): 
    '''Waits watchtime seconds. Returns True on click, False otherwise''' 
    if button in (1, '1', 'l', 'L', 'left', 'Left', 'LEFT'): 
     bnum = 0x01 
    elif button in (2, '2', 'r', 'R', 'right', 'Right', 'RIGHT'): 
     bnum = 0x02 

    start = time.time() 
    while 1: 
     if ctypes.windll.user32.GetKeyState(bnum) not in [0, 1]: 
      #^this returns either 0 or 1 when button is not being held down 
      return True 
     elif time.time() - start >= watchtime: 
      break 
     time.sleep(0.001) 
    return False