# Cross platform keyboard input

**URL:** <https://discuss.python.org/t/cross-platform-keyboard-input/51979>\
**Category:** Python Help\
**Created:** [April 26, 2024, 12:44pm UTC](https://discuss.python.org/t/cross-platform-keyboard-input/51979 "2024-04-26T12:44:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nineteendo](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/nineteendo/32/19122_2.png) [@Nineteendo](https://discuss.python.org/u/Nineteendo)\
**Post date:** [April 26, 2024, 12:44pm UTC](https://discuss.python.org/t/cross-platform-keyboard-input/51979/1 "2024-04-26T12:44:09Z")

</div>

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 output
- `pip install keyboard`: requires root on macOS, can’t be included with your code and acts like a keylogger on Windows
- `pip install pynput`: not trusted on macOS and can’t be included with your code

So, I wrote my own, [AnsI/O](https://pypi.org/project/ansio), 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

```python
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:

```none
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

```python
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:

![colored_output](https://us1.discourse-cdn.com/flex002/uploads/python1/original/3X/2/c/2c4df6227966e284b0d6122ed2f246597742e437.png)

---

<div class="post-metadata">

**Author:** ![JamesParrott](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jamesparrott/32/10534_2.png) [@JamesParrott](https://discuss.python.org/u/JamesParrott)\
**Post date:** [April 26, 2024, 1:44pm UTC](https://discuss.python.org/t/cross-platform-keyboard-input/51979/2 "2024-04-26T13:44:28Z")

</div>

Nice work. I like a simple, well thought out pure Python library.

It’s fairly large, but does PyGame require root, trust, or impose restrictions etc.?  
[https://www.pygame.org/docs/ref/key.html](https://www.pygame.org/docs/ref/key.html)

---

<div class="post-metadata">

**Author:** ![Nineteendo](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/nineteendo/32/19122_2.png) [@Nineteendo](https://discuss.python.org/u/Nineteendo)\
**Post date:** [April 26, 2024, 2:12pm UTC](https://discuss.python.org/t/cross-platform-keyboard-input/51979/3 "2024-04-26T14:12:48Z")

</div>

> [@JamesParrott](#):
>
> It’s fairly large, but does PyGame require root, trust, or impose restrictions etc.?

1. It can’t be included with my code (must be installed because it’s partially written in C).
2. It must be used without a window (not in the command line)
