python keyboard does not recognize keys

990 Views Asked by At

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'))
1

There are 1 best solutions below

2
Guf On

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.