python怎么识别鼠标的动作_使用python检测窗口中的鼠标点击

在程序外部检测鼠标事件的唯一方法是使用

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脚本,以获取有关event参数的更多信息。

因为pyHook需要一个活动的消息泵,所以pyHook可能会在纯Python脚本中使用很棘手。从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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值