'Python keyboard module non-functional with python 3.x and macOS?

So I'm trying to write a macro to automate a few tedious actions on my mac. I've been attempting to use the keyboard module for python (so that I can hook into global key events), but although it installs properly, it doesn't seem to function in its entirety.

Here's what I've tried so far, and the result of those attempts:

  1. Installing keyboard through pip install keyboard with python 3.10.0. This works, and I can import keyboard successfully in my python scripts. However, when I try to simulate a keypress (such as by use of keyboard.press('a')) I get this error:
Python 3.10.0 (v3.10.0:b494f5935c, Oct  4 2021, 14:59:19) [Clang 12.0.5 (clang-1205.0.22.11)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import keyboard
>>> keyboard.press('a')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/keyboard/__init__.py", line 396, in press
    send(hotkey, True, False)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/keyboard/__init__.py", line 379, in send
    parsed = parse_hotkey(hotkey)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/keyboard/__init__.py", line 344, in parse_hotkey
    scan_codes = key_to_scan_codes(hotkey)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/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'))

It might be helpful to note that keyboard.write('a') works totally fine. It's just when keyboard tries to interpret 'a' as a key that something goes wrong, and I have no idea why this is. Also, keyboard.press('esc') works fine too, and so do any special keys like it. It just seems to be the alphanumerical keys that are broken.

  1. I've also tried installing the keyboard module through a fresh install of python 3.9 just in case it's a 3.10 issue, but that brings with it another can of worms... First of all, I can't install it using pip install keyboard or pip3 install keyboard because both default to 3.10 python installations. So I install using python3.9 -m pip install keyboard. However, this is what happens when I try to import it:
Python 3.9.8 (v3.9.8:bb3fdcfe95, Nov  5 2021, 16:40:46) 
[Clang 13.0.0 (clang-1300.0.29.3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import keyboard
zsh: segmentation fault  python3.9

What is even going on anymore?? I've tried clearing all my pip packages using pip3 freeze | xargs pip3 uninstall -y and then reinstalling keyboard, but that fixes absolutely nothing. And from what I've researched online, basically no one else is having this issue. I suspect my python installations are somehow messed up, but what's up with the error on the fresh installation of 3.9.8, which should be supported according to the documentation? My last ditch attempt at explaining why this is happening is that I'm running on Apple Silicon but I highly doubt that has anything to do with it...

TLDR; At the end of the day, I just want to write a script that can detect global key events and then submit custom global key and mouse events that I specify. Every other python module I've tried has broken down in some form, usually because they were built for windows. At this point I'm willing to accept defeat in the fight against the keyboard module, but I still want to find workarounds for accomplishing my task. I don't even care whether it's in python, any working alternative that gives me freedom in scripting the macro would be appreciated at this point.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source