Python全局捕获和处置鼠标点击事件

发布于 2025-01-17 19:44:32 字数 635 浏览 3 评论 0原文

我想创建一个工具,该工具使我可以使用鼠标将密钥发送到活动窗口。

假设我单击左鼠标按钮时,我想发送键“ a”。但是我也想抑制 /处理该特定的点击事件。因此,目标应用只会感觉到键盘输入“ A”,而不会感觉到鼠标事件的左键单击。

使用以下代码,我可以看到鼠标点击。但是我想处置 /停止活动,而不是系统处理。

from pynput import mouse

def do_something():
    print("something")

def on_click(x, y, button, pressed):
    print('{0} {1} at {2}'.format(button, 'Pressed' if pressed else 'Released', (x, y)))
    if (button.value == 1):
        do_something()
        #suppress / dispose the click event...

# Collect events until released
with mouse.Listener( on_click=on_click ) as listener:
    listener.join()

顺便说一句,我正在使用Ubuntu 20.04。 提前致谢。

I want to create a tool, which allows me to send keys to active window using my mouse.

Let's say I want to send the key "A" when I click the left mouse button. But I also want to suppress / dispose that particular click event. So the target app will only feel the keyboard input "A", but not the left click of the mouse event.

With the following code I am able to see the mouse clicks. But I want to dispose / stop the event and not processed by the system.

from pynput import mouse

def do_something():
    print("something")

def on_click(x, y, button, pressed):
    print('{0} {1} at {2}'.format(button, 'Pressed' if pressed else 'Released', (x, y)))
    if (button.value == 1):
        do_something()
        #suppress / dispose the click event...

# Collect events until released
with mouse.Listener( on_click=on_click ) as listener:
    listener.join()

By the way I am using Ubuntu 20.04.
Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娇纵 2025-01-24 19:44:32

我找到了一种抑制所有点击事件的方法。

from pynput import mouse

def do_something():
    print("something")

def on_click(x, y, button, pressed):
    print('{0} {1} at {2}'.format(button, 'Pressed' if pressed else 'Released', (x, y)))
    if (button.value == 1):
        do_something()
        #suppress / dispose the click event...

# Collect events until released
with mouse.Listener( on_click=on_click, suppress=True ) as listener:
    listener.join()

我仍在寻找一种解决方案来抑制某个按钮的点击。

I found a way that suppress all the click events.

from pynput import mouse

def do_something():
    print("something")

def on_click(x, y, button, pressed):
    print('{0} {1} at {2}'.format(button, 'Pressed' if pressed else 'Released', (x, y)))
    if (button.value == 1):
        do_something()
        #suppress / dispose the click event...

# Collect events until released
with mouse.Listener( on_click=on_click, suppress=True ) as listener:
    listener.join()

I am still looking for a solution to suppress a certain button click.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文