Skip to content

Instantly share code, notes, and snippets.

@alexhsamuel
Created November 6, 2019 02:09
Show Gist options
  • Save alexhsamuel/0565a55d10913143646a7b96545660f7 to your computer and use it in GitHub Desktop.
Save alexhsamuel/0565a55d10913143646a7b96545660f7 to your computer and use it in GitHub Desktop.
stuff TTY input
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