我是Python的新手,我想编写一个监听左,右或中间(鼠标滚轮)按钮单击事件的脚本。单击鼠标左键或右键时,它不应执行任何操作。换句话说,脚本应该阻止鼠标左键和右键单击。
但是当点击鼠标中键时,脚本应该按下退出按钮。
我已经找到了一个用于监听鼠标事件的库:Pynput。问题是该脚本必须在Windows XP机器上运行,所以我必须使用Python 3.4.4并且我找不到这个Python版本的Pynput版本。
我现在的问题:如何在Windows 3.4P的Python 3.4.4中听鼠标点击并发送键盘事件?
更新
现在Pynput已正确安装并正在运行。我写了这个:
from pynput import mouse
from pynput.keyboard import Key, Controller
keyboard = Controller()
def on_click(x, y, button, pressed):
if button == mouse.Button.middle:
keyboard.press(Key.esc)
keyboard.release(Key.esc)
#after pressing the esc-key: stopPropagation(middle mouse click should not be forwarded to windows)
if button == mouse.Button.left:
print("left")
#stopPropagation(left mouse click should not be forwarded to windows)
if button == mouse.Button.right:
print("right")
#stopPropagation(right mouse click should not be forwarded to windows)
# Collect events until released
with mouse.Listener(
on_click=on_click) as listener:
listener.join()
我如何完成我在#评论中所写的内容?甚至可以做一些事情,比如防止用Python在鼠标中点击鼠标?另外我不明白的是,当我运行该程序并且我想要退出它时(使用ctrl + c)我必须按100次然后它会显示:
Traceback (most recent call last):
File "C:\Users\path\hello.py", line 22, in <module>
File "C:\Python34\lib\site-packages\pynput-1.3.10-py3.6.egg\pynput\_util\__init__.py", line 178, in join
File "C:\Python34\lib\threading.py", line 1060, in join
self._wait_for_tstate_lock()
File "C:\Python34\lib\threading.py", line 1076, in _wait_for_tstate_lock
elif lock.acquire(block, timeout):
KeyboardInterrupt
为什么呢?我该如何解决这个问题?