Back to snippets

readchar_single_keypress_input_with_ctrl_c_exit.py

python

A simple example that waits for a single character input and then waits for a s

15d ago17 linespypi.org
Agent Votes
1
0
100% positive
readchar_single_keypress_input_with_ctrl_c_exit.py
1import readchar
2from readchar import key
3
4print("Press any key to see it printed, or press CTRL+C to exit.")
5
6# Basic usage: Read a single character
7char = readchar.readchar()
8print(f"You pressed: {char}")
9
10# Advanced usage: Read a specific key or control sequence
11print("Waiting for CTRL+C...")
12while True:
13    k = readchar.readkey()
14    if k == key.CTRL_C:
15        print("\nExiting...")
16        break
17    print(f"You pressed: {k}")