【问题标题】:On right click with pynput右键单击pynput
【发布时间】:2020-11-21 21:27:52
【问题描述】:

我正在尝试使用 pynput 的 on_click() 函数全局检测右键单击并在该右键单击上运行事件。

这是我目前的代码:

import pynput
from pynput.mouse import Listener, Button, Controller

mouse = Controller()

def on_click(x, y, button, pressed):
    if pressed:
        print("Click Detected")

with Listener(on_click=on_click) as listener:
    listener.join()

当按下任何鼠标按钮(例如鼠标中键或鼠标侧键)时,此代码将“检测到单击”打印到控制台。我正在尝试让它只在我右键单击时打印。

我尝试传递mouse.Button.right 而不是button,但它给了我一个语法错误并突出显示“。”,我还尝试使用@ 将mouse.Button.right 传递给button 变量987654326@ 但它给了我一个AttributeError: 'Controller' object has no attribute 'Button' 错误。

如果有人了解 pynput 库或知道什么可以为此工作,那就太好了。我是 python 新手,所以任何建设性的批评都会很棒。

【问题讨论】:

    标签: python pynput


    【解决方案1】:

    我建议您尝试使用鼠标库,因为它更容易。我认为你可以在那里做同样的事情,只使用一行代码mouse.is_pressed("right") 如果你想安装那个库,只需在 cmd 中使用 pip install mouse

    【讨论】:

      【解决方案2】:

      同时检查变量 'button' 是否等于 'mouse.Button.middle':

      from pynput import mouse
      
      
      def on_click(x, y, button, pressed):
          if not pressed and button == mouse.Button.middle:
              print("Right Click Detected (released)")
      
      
      with mouse.Listener(on_click=on_click) as listener:
          listener.join()
      

      【讨论】:

        猜你喜欢
        • 2021-09-05
        • 2012-11-06
        • 1970-01-01
        • 2011-01-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多