2024-01-22

Keyboard input with pynput on a Mac

keyboard fails on a Mac and I would like to use pynput but fail to execute successfully.

from pynput import keyboard
import time

def on_press(key, ts, gt_forward, rb, extent, aoi, aoibuffer, jparams):
    if key == keyboard.Key.enter:
        print("\nYou pressed Enter... we continue with osm_LoD1_3DCityModel.")
        
        ...lots of code that saves something.
        
def on_release(key):
    if key == keyboard.Key.esc:
        # Stop listener
        return False

def main():
    start = time.time()
    
    ...code here...

    print('')
    print("Have a look at the matplotlib render that might highlight errors (red) in the 2D vectors.\n\nIf there are no challenges press 'Enter' to continue; else press 'Esc' to exit: ")

    with keyboard.Listener(
            on_press=lambda key: on_press(key, ts, gt_forward, rb, extent, aoi, aoibuffer, jparams),
            on_release=keyboard) as listener:
        listener.join()

    src_ds = None      
    end = time.time()
    print('runtime:', str(timedelta(seconds=(end - start))))  

if __name__ == "__main__":
    main()

I need to pass the ts, gt_forward, rb, extent, aoi, aoibuffer, jparams variables to on_pass function ---...lots of code that saves something. will use it-- but am not sure of the proper syntax. I get an error message.

  File ~/miniconda3/envs/osm3D_v3/lib/python3.8/site-packages/spyder_kernels/py3compat.py:356 in compat_exec
    exec(code, globals, locals)

  File ~/Documents/osm_LoD1_3DCityModel/Jan2024_fromGitHub/osm3DMainMac.py:178
    main()

  File ~/Documents/osm_LoD1_3DCityModel/Jan2024_fromGitHub/osm3DMainMac.py:155 in main
    listener.join()

  File ~/miniconda3/envs/osm3D_v3/lib/python3.8/site-packages/pynput/_util/__init__.py:276 in join
    six.reraise(exc_type, exc_value, exc_traceback)

  File ~/miniconda3/envs/osm3D_v3/lib/python3.8/site-packages/six.py:718 in reraise
    raise value.with_traceback(tb)

  File ~/miniconda3/envs/osm3D_v3/lib/python3.8/site-packages/pynput/_util/__init__.py:228 in inner
    return f(self, *args, **kwargs)

  File ~/miniconda3/envs/osm3D_v3/lib/python3.8/site-packages/pynput/_util/darwin.py:265 in _handler
    self._handle(proxy, event_type, event, refcon)

  File ~/miniconda3/envs/osm3D_v3/lib/python3.8/site-packages/pynput/keyboard/_darwin.py:273 in _handle
    self.on_release(key)

  File ~/miniconda3/envs/osm3D_v3/lib/python3.8/site-packages/pynput/_util/__init__.py:144 in inner
    if f(*args) is False:

TypeError: 'module' object is not callable

How do I structure the function call successfully please?

Also will the revised script, with pynput, execute successfully on a Windows machine?



No comments:

Post a Comment