File: debug_input_cross_platform.py

package info (click to toggle)
prompt-toolkit 3.0.51-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,232 kB
  • sloc: python: 30,894; makefile: 151; sh: 6
file content (32 lines) | stat: -rwxr-xr-x 622 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/env python
"""
Read input and print keys.
For testing terminal input.

Works on both Windows and Posix.
"""

import asyncio

from prompt_toolkit.input import create_input
from prompt_toolkit.keys import Keys


async def main() -> None:
    done = asyncio.Event()
    input = create_input()

    def keys_ready():
        for key_press in input.read_keys():
            print(key_press)

            if key_press.key == Keys.ControlC:
                done.set()

    with input.raw_mode():
        with input.attach(keys_ready):
            await done.wait()


if __name__ == "__main__":
    asyncio.run(main())