I couldn’t find a cross platform library for keyboard input that doesn’t also need special priviliges:
curses: doesn’t work on Windows and emposes arbitrary restrictions on outputpip install keyboard: requires root on macOS, can’t be included with your code and acts like a keylogger on Windowspip install pynput: not trusted on macOS and can’t be included with your code
So, I wrote my own, AnsI/O, providing a low-level interface for input & output. Whenever the program is terminated or suspended (Unix only), it restores the terminal back to the default settings.
But because it’s so early, I’m currently looking for feedback.
Example usage
Keyboard input
from ansio import application_keypad, mouse_input, raw_input
from ansio.input import InputEvent, get_input_event
with raw_input, application_keypad, mouse_input:
while True:
event: InputEvent = get_input_event()
print(event.pressed, repr(event.shortcut))
Example output:
True 'alt+shift+a'
True 'ctrl+a'
True 'tab'
True 'shift+up'
True 'a'
True 'primary_click'
False 'primary_click'
True 'ctrl+primary_click'
False 'ctrl+primary_click'
Colored output
from ansio import colored_output
from ansio.colors import blue_bg, invert, italic, yellow
with colored_output:
print(f'Print in {yellow(blue_bg("color"))} and with {italic(invert("styles"))}')
Example output:
![]()