如何在Linux上使用Python 3检测鼠标点击?

5
我很新于Python,我想能够检测全屏幕的鼠标点击事件。
这个问题最接近我的需求,但是其中的回答都不是很详细。
请问该如何实现?

7
无论你问的是什么,我都可能投赞成票,因为你看起来像 Gandalf 在提出一个 Linux 问题,但我也认为这个问题相关。 - temporary_user_name
也许这可以帮助:链接 但是这需要sudo权限。 - Simon Sagi
1个回答

8
你可以使用库PyUserInput处理鼠标输入(来自GitHub的代码示例):
from pymouse import PyMouseEvent

def fibo():
    a = 0
    yield a
    b = 1
    yield b
    while True:
        a, b = b, a+b
        yield b

class Clickonacci(PyMouseEvent):
    def __init__(self):
        PyMouseEvent.__init__(self)
        self.fibo = fibo()

    def click(self, x, y, button, press):
        '''Print Fibonacci numbers when the left click is pressed.'''
        if button == 1:
            if press:
                print(self.fibo.next())
        else:  # Exit if any other mouse button used
            self.stop()

C = Clickonacci()
C.run()

否则,您可以使用Xlib库完成此操作:Python Xlib捕获/发送鼠标点击

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接