这是我的代码的键盘和鼠标监听器部分:
python
def mouse_listener(scroller):
"""Listens for mouse scroll events to scroll clipboard history."""
logging.info("Started mouse listener.")
def on_scroll(x, y, dx, dy):
try:
if dy > 0:
scroller.scroll_clipboard_history("up")
else:
scroller.scroll_clipboard_history("down")
except Exception as e:
logging.error(f"Error in mouse_listener in on_scroll: {e}")
try:
with mouse.Listener(on_scroll=on_scroll) as listener:
listener.join()
except Exception as e:
logging.error(f"Error in mouse_listener (join()): {e}")
logging.info("Stopped mouse listener.")
def keyboard_listener(scroller):
"""Listens for keyboard events."""
logging.info("Started keyboard listener.")
def on_press(key):
try:
if key == keyboard.Key.f9: # Stop the program
stop_clipboard_scroller()
return False # Stops the listener when F9 is pressed
elif key == keyboard.Key.ctrl and not stop_flag.is_set(): # Show ghost on Ctrl press
scroller.show_ghost()
elif key == keyboard.Key.fromchar('v') and not stop_flag.is_set(): # Check for Ctrl+V
scroller.show_ghost() # Show ghost on Ctrl+V press
except Exception as e:
logging.error(f"Error in keyboard_listener in on_press: {e}")
在我的日志文件中我得到:
plaintext
INFO - Started mouse listener.
INFO - Started keyboard listener.
ERROR - Unhandled exception in listener callback
Traceback (most recent call last):
File "...\pynput\_util\win32.py", line 386, in _handler
converted = self._convert(code, msg, lpdata)
File "...\pynput\_util\win32.py", line 401, in _convert
raise NotImplementedError()
NotImplementedError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "...\pynput\_util\__init__.py", line 229, in inner
return f(self, *args, **kwargs)
File "...\pynput\_util\win32.py", line 390, in _handler
self._handle(code, msg, lpdata)
~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
TypeError: '_thread._ThreadHandle' object is not callable
2024-12-07 03:01:12,374 - INFO - Stopped mouse listener.
正如您所看到的,我的日志记录错误都没有被调用,所以我不知道在这里该怎么做。在我用 google 搜索后,我只在 github 上找到了 2023 年的一个不相关的错误报告。任何有关此问题的帮助将不胜感激。
这是我的第一篇文章,如果我没有正确地提出我的问题,我深表歉意。
运行文件时出现错误。我希望他们不会。
由于命名冲突,这似乎是 pynput 中的一个已知错误。用户发布了修复程序。这是链接:https:
//github.com/moses-palmer/pynput/tree/fixup/listener-thread-handle
已提交更改列表:
[Rename _handle for xorg listener,
Rename _handle for uinput listener,
Rename _handle for win32 listener,
Rename _handle for darwin listener]