0

Here is the keyboard and mouse listener part of my code:

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}")

And in my log file I get:

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.

As you can see none of my logging errors were called so I don't know what to do here. After I googled it I only found one unrelated bug report on github from 2023. Any help with this would be greatly appreciated.

This is my first post so I apologise if I haven't set out my question correctly.

The errors occur when I run the file. I expect them not to.

1 Answer 1

0

It seems it's a know bug in pynput due to naming conflicts. A fix was posted by a user. Here is the link: https:

//github.com/moses-palmer/pynput/tree/fixup/listener-thread-handle

List of committed changes:

[Rename _handle for xorg listener, 
Rename _handle for uinput listener, 
Rename _handle for win32 listener,
Rename _handle for darwin listener]

Not the answer you're looking for? Browse other questions tagged or ask your own question.