如何使用Python检测鼠标侧键 [英] How to detect mouse side buttons with Python
问题描述
我一直在使用pyhook和click事件的message
属性,但是它似乎只能检测到三个标准按钮.
其他人甚至没有找到处理程序.
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确实钩住了XButton,PyHook的dll将其传递给python,但是python端的HookManager忽略了它们.但是,您可以跳过HookManager并直接使用cpyHook界面.
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.
此示例在按钮被按下时将xbutton事件打印到控制台,并在按下鼠标左键时退出:
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)
这篇关于如何使用Python检测鼠标侧键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!