import select import sys import termios import tty def disable_cursor(tty): with open(tty, 'w') as t: t.write("\033[?25l") def enable_cursor(tty): with open(tty, 'w') as t: t.write("\033[?25h") def restore_screen(tty): with open(tty, 'w') as t: t.write("\033[?5h\033[?5l") def was_key_pressed(): # Save the terminal settings fd = sys.stdin.fileno() old_settings = termios.tcgetattr(fd) try: # Set the terminal to raw mode to read key press tty.setraw(sys.stdin.fileno()) # Use select to check if there's any input rlist, _, _ = select.select([sys.stdin], [], [], 0) finally: # Restore the terminal settings termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) return bool(rlist)