python怎么判断鼠标左右,如何使用Python检测鼠标侧键

I've been using pyhook and the message attribute of the clicking events, but it seems to be able to detect just the three standard buttons.

The others don't even get to the handler.

Is there a way to detect the extra buttons that the mouse might have?

解决方案

WH_MOUSE_LL does hook XButtons and PyHook's dll passes it through to python, but the HookManager on the python side ignores them. However, you can skip HookManager and use the cpyHook interface directly.

This example prints xbutton events to the console as they are pressed and exits when the left mouse button is pressed:

from pyHook import cpyHook, HookConstants

import pythoncom

import ctypes

user32 = ctypes.windll.user32

XBUTTON1 = 0x0001

XBUTTON2 = 0x0002

wm = { 0x020B: "WM_XBUTTONDOWN", 0x020C: "WM_XBUTTONUP", 0x0201: "WM_LBUTTONDOWN", }

def mouse_handler(msg, x, y, data, flags, time, hwnd, window_name):

name = wm.get(msg, None)

if name:

xb = data >> 16 # high word indicates which xbutton

print(name, xb & XBUTTON1, xb & XBUTTON2)

if name == "WM_LBUTTONDOWN":

user32.PostQuitMessage(0)

return True # True = pass the event to other handlers

try:

cpyHook.cSetHook(HookConstants.WH_MOUSE_LL, mouse_handler)

pythoncom.PumpMessages() # returns on WM_QUIT

finally:

cpyHook.cUnhook(HookConstants.WH_MOUSE_LL)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值