Created
November 6, 2019 02:09
-
-
Save alexhsamuel/0565a55d10913143646a7b96545660f7 to your computer and use it in GitHub Desktop.
stuff TTY input
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fcntl | |
import os | |
import sys | |
import termios | |
def stuff(text): | |
tty_name = os.ttyname(sys.stdin.fileno()) | |
tty_fd = os.open(tty_name, os.OWRONLY) | |
try: | |
old_attr = termios.tcgetattr(tty_fd) | |
new_attr = list(old_attr) | |
new_attr[3] &= ~termios.ECHO | |
termios.tcsetattr(tty_fd, termios.TCSANOW, new_attr) | |
try: | |
for c in text: | |
fcntl.ioctl(tty_fd, termios.TIOCSTI, c) | |
finally: | |
termios.tcsetattr(tty_fd, termios.TCSANOW, old_attr) | |
finally: | |
os.close(tty_fd) | |
stuff("Hello, world!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment