0

I am trying to detect the keys being pressed in python with "keyboard" but apparently it does not recognize the keys.
(I use python 3.11.0 with macOs ventura 13.0)

my code

import keyboard
while True:
    if keyboard.is_pressed("a"):
        print("You pressed 'a'.")
        break

and I get this error

Traceback (most recent call last):
  File "/Users/user/Desktop/zzz.py", line 3, in <module>
    if keyboard.is_pressed("a"):
       ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/keyboard/__init__.py", line 417, in is_pressed
    steps = parse_hotkey(hotkey)
            ^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/keyboard/__init__.py", line 344, in parse_hotkey
    scan_codes = key_to_scan_codes(hotkey)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/keyboard/__init__.py", line 324, in key_to_scan_codes
    raise ValueError('Key {} is not mapped to any known key.'.format(repr(key)), e)
ValueError: ("Key 'a' is not mapped to any known key.", ValueError('Unrecognized character: a'))
Ale
  • 17
  • 9

1 Answers1

1

It can work well in Windows and Linux. But it is experimental on macos. There may be many bugs or no support. The author of the keyboard-module has declared, Details boppreh/keyboard.

Guf
  • 149
  • 1
  • 7
  • so what module could i use from macOs that works as keyboard? – Ale Nov 09 '22 at 18:17
  • [PyUserInput](https://pypi.org/project/PyUserInput/) is a good choice, but it is too old. It can be used in simple scenarios. It is recommended to use [pyobjc](https://pypi.org/project/pyobjc/) in complex scenarios, but it will increase the programming difficulty a little. – Guf Nov 10 '22 at 02:48