使用python检测Windows中的鼠标单击

mon*_*kut 21 python windows mouse

无论鼠标位于哪个窗口,我如何检测鼠标点击?

在python中Perferabliy,但是如果有人可以在任何语言中解释它我可能能够弄明白.

我在microsoft的网站上找到了这个: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()
Run Code Online (Sandbox Code Playgroud)

这只会返回0,0.我不熟悉pygame,缺少什么?

无论如何,我更喜欢不需要安装第三方模块的方法.(除了pywin32 http://sourceforge.net/projects/pywin32/)

efo*_*nis 31

检测程序外部鼠标事件的唯一方法是使用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()
Run Code Online (Sandbox Code Playgroud)

您可以查看随模块一起安装的example.py脚本,以获取有关事件参数的更多信息.

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

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


小智 17

我用的是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)
Run Code Online (Sandbox Code Playgroud)


gim*_*mel 5

Windows MFC(包括 GUI 编程)可通过 Mark Hammond 的Python for Windows 扩展通过 python 进行访问。O'Reilly 的书摘录自 Hammond 和 Robinson 的,展示了如何挂钩鼠标消息,例如:

self.HookMessage(self.OnMouseMove,win32con.WM_MOUSEMOVE)
Run Code Online (Sandbox Code Playgroud)

原始 MFC 并不容易或显而易见,但在 Web 上搜索 Python 示例可能会产生一些可用的示例。


dil*_*gar 5

自从提出这个问题以来已经很热了,但我想我会分享我的解决方案:我只是使用了内置模块ctypes。(顺便说一句,我正在使用 Python 3.3)

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
Run Code Online (Sandbox Code Playgroud)