在 Python 鼠标模块中使用回调(检索事件类型)
Using Callbacks in Python Mouse Module (retrieving event types)
我正在使用 Python 模块 "Mouse" 来捕获鼠标输入(更具体地说,鼠标坐标、输入按钮和输入类型)。
资料来源:https://github.com/boppreh/mouse
查看 on_button() 和 on_click() 的源代码,我不确定如何将 event_type 传回 "mouse button down" 和 "mouse button up" 回到我的代码:
def left_mouse():
print('LEFT ', mouse.get_position())
# I want to get the [mouse button, example: left] and [type, example: up] from the logging
mouse.on_click(left_mouse)
来自鼠标模块的源代码:on_button 和 on_click;我猜我没有适当地调用回调,但无法弄清楚什么是正确的方法:
def on_button(callback, args=(), buttons=(LEFT, MIDDLE, RIGHT, X, X2), types=(UP, DOWN, DOUBLE)):
""" Invokes `callback` with `args` when the specified event happens. """
if not isinstance(buttons, (tuple, list)):
buttons = (buttons,)
if not isinstance(types, (tuple, list)):
types = (types,)
def handler(event):
if isinstance(event, ButtonEvent):
if event.event_type in types and event.button in buttons:
callback(*args)
_listener.add_handler(handler)
return handler
def on_click(callback, args=()):
""" Invokes `callback` with `args` when the left button is clicked. """
return on_button(callback, args, [LEFT], [UP])
一如既往的感谢。
因此,根据上面的评论,如果事件是 "up" 或 "down"。
,您正在努力检索
本质上,不需要向您提供此信息,因为您正在使用的此功能有一项特定的工作。 event_up (在我的评论中我说它是鼠标按下,但它总是向上) 并且总是 "left mouse button"。意思是,没有必要向您提供此信息 - 因为您正在使用的库函数,on_click()
是一条没有任何可变结果的单向街道。
您可以试试这个,方法是执行您的代码并按住鼠标左键,在您松开按钮之前什么也不会发生。那是 left+up 触发的时候——唯一可以来自 on_click()
.
的结果
您遇到的问题可能是您试图附加函数 left_mouse()
来处理所有鼠标事件?
通常,您会为每个事件附加一个唯一的函数 - 就像您在代码中使用 left_mouse()
一样。但是,如果您出于某种原因需要合并它们,这是可行的方法:
def mouse(event):
print('Position:', mouse.get_position())
print('Event:', event)
mouse.on_click(left_mouse, args=('mouse_left', 'mouse_up'))
mouse.on_righ_click(left_mouse, args=('mouse_right', 'mouse_up'))
但是,如果您想将一个函数与用于不同鼠标操作的预定义库函数结合使用,这又是一个问题。
您可能感兴趣的是 class mouse.ButtonEvent
。此事件具有附加的触发器和信息。但是要得到这个 event/object 你需要挂钩并让图书馆知道你想负责每个事件。这意味着您不能使用 left-mouse-release 等的预构建函数,您需要构建自己的事件处理程序。您可以通过调用 mouse.hook
然后调用您希望在每次鼠标事件时调用的函数来执行此操作。
例如:
import mouse
import time
events = []
mouse.hook(events.append)
while 1:
mouse._listener.queue.join()
for event in events:
print(event)
del events[:]
time.sleep(0.25)
您注册函数 events.append
(常规列表追加函数) 以在每次鼠标移动时调用。 (您也可以将其替换为您自己的函数,并直接在函数中处理事件,就像您尝试做的一样).
从那里,您可以访问每个 "frame" 或鼠标事件并相应地执行操作。
所有这些都可以在图书馆自己的 test cases 中找到,了解他们如何测试每个功能。
这一切都不是您要寻找的第一件事,而且它在 "how to get started".
方面没有记录
我正在使用 Python 模块 "Mouse" 来捕获鼠标输入(更具体地说,鼠标坐标、输入按钮和输入类型)。 资料来源:https://github.com/boppreh/mouse
查看 on_button() 和 on_click() 的源代码,我不确定如何将 event_type 传回 "mouse button down" 和 "mouse button up" 回到我的代码:
def left_mouse():
print('LEFT ', mouse.get_position())
# I want to get the [mouse button, example: left] and [type, example: up] from the logging
mouse.on_click(left_mouse)
来自鼠标模块的源代码:on_button 和 on_click;我猜我没有适当地调用回调,但无法弄清楚什么是正确的方法:
def on_button(callback, args=(), buttons=(LEFT, MIDDLE, RIGHT, X, X2), types=(UP, DOWN, DOUBLE)):
""" Invokes `callback` with `args` when the specified event happens. """
if not isinstance(buttons, (tuple, list)):
buttons = (buttons,)
if not isinstance(types, (tuple, list)):
types = (types,)
def handler(event):
if isinstance(event, ButtonEvent):
if event.event_type in types and event.button in buttons:
callback(*args)
_listener.add_handler(handler)
return handler
def on_click(callback, args=()):
""" Invokes `callback` with `args` when the left button is clicked. """
return on_button(callback, args, [LEFT], [UP])
一如既往的感谢。
因此,根据上面的评论,如果事件是 "up" 或 "down"。
,您正在努力检索
本质上,不需要向您提供此信息,因为您正在使用的此功能有一项特定的工作。 event_up (在我的评论中我说它是鼠标按下,但它总是向上) 并且总是 "left mouse button"。意思是,没有必要向您提供此信息 - 因为您正在使用的库函数,on_click()
是一条没有任何可变结果的单向街道。
您可以试试这个,方法是执行您的代码并按住鼠标左键,在您松开按钮之前什么也不会发生。那是 left+up 触发的时候——唯一可以来自 on_click()
.
您遇到的问题可能是您试图附加函数 left_mouse()
来处理所有鼠标事件?
通常,您会为每个事件附加一个唯一的函数 - 就像您在代码中使用 left_mouse()
一样。但是,如果您出于某种原因需要合并它们,这是可行的方法:
def mouse(event):
print('Position:', mouse.get_position())
print('Event:', event)
mouse.on_click(left_mouse, args=('mouse_left', 'mouse_up'))
mouse.on_righ_click(left_mouse, args=('mouse_right', 'mouse_up'))
但是,如果您想将一个函数与用于不同鼠标操作的预定义库函数结合使用,这又是一个问题。
您可能感兴趣的是 class mouse.ButtonEvent
。此事件具有附加的触发器和信息。但是要得到这个 event/object 你需要挂钩并让图书馆知道你想负责每个事件。这意味着您不能使用 left-mouse-release 等的预构建函数,您需要构建自己的事件处理程序。您可以通过调用 mouse.hook
然后调用您希望在每次鼠标事件时调用的函数来执行此操作。
例如:
import mouse
import time
events = []
mouse.hook(events.append)
while 1:
mouse._listener.queue.join()
for event in events:
print(event)
del events[:]
time.sleep(0.25)
您注册函数 events.append
(常规列表追加函数) 以在每次鼠标移动时调用。 (您也可以将其替换为您自己的函数,并直接在函数中处理事件,就像您尝试做的一样).
从那里,您可以访问每个 "frame" 或鼠标事件并相应地执行操作。
所有这些都可以在图书馆自己的 test cases 中找到,了解他们如何测试每个功能。
这一切都不是您要寻找的第一件事,而且它在 "how to get started".