moses-palmer / pynput

Sends virtual input commands
GNU Lesser General Public License v3.0
1.78k stars 248 forks source link

KeyError when trying to start 2 listeners #552

Open ppraisethesun opened 1 year ago

ppraisethesun commented 1 year ago

Description Getting a KeyError when trying to start 2 listeners

Platform and pynput version Mac OS 13.3.1 (22E261) Python 3.11.3 Name: pynput Version: 1.7.6

To Reproduce

from pynput import keyboard

def on_press(key):
    try:
        pass
    except:
        pass

def on_release(key):
    try:
        pass
    except:
        pass

with keyboard.Listener(on_press=on_press, on_release=on_release) as l1, \
        keyboard.Listener(on_press=on_press, on_release=on_release) as l2:
    l1.join()
    l2.join()
Unhandled exception in listener callback
Traceback (most recent call last):
  File "/opt/homebrew/lib/python3.11/site-packages/pynput/_util/__init__.py", line 228, in inner
    return f(self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/pynput/_util/darwin.py", line 265, in _handler
    self._handle(proxy, event_type, event, refcon)
  File "/opt/homebrew/lib/python3.11/site-packages/pynput/keyboard/_darwin.py", line 262, in _handle
    key = self._event_to_key(event)
          ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/pynput/keyboard/_darwin.py", line 320, in _event_to_key
    vk = Quartz.CGEventGetIntegerValueField(
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/objc/_lazyimport.py", line 168, in __getattr__
    value = getattr(p, name)
            ^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/objc/_lazyimport.py", line 186, in __getattr__
    value = self.__get_constant(name)
            ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/objc/_lazyimport.py", line 412, in __get_constant
    self.__funcmap.pop(name)
KeyError: 'CGEventGetIntegerValueField'
moses-palmer commented 1 year ago

Thank you for your report.

Could you please try the version on the master branch? The lazy imports have been removed, and they appear to be a problem for pyobjc.

rafaelBauer commented 10 months ago

I am having the same problem.

I am starting a "listener" and "hotkeys", and I am getting the same error.

I used the master branch and the problem seems to be solved. Is there a forecast when the next release will be?

catbox305 commented 5 months ago

Same thing here.