Closed altendky closed 2 years ago
This looks like it would work, but all that's really required is to change tty.setraw()
to tty.setcbreak()
in the library readchar()
function. Since it's only a few lines, you can make a local copy of it:
def readchar():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
# tty.setraw(sys.stdin.fileno())
tty.setcbreak(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
I hit this same issue. Here's my workaround that may help people:
# Read a single character using readchar(). This is a blocking call.
character = readchar.readchar()
# Note: readchar() currently intercepts CTRL-C etc [1]. The easiest solution is to detect CTRL-C and simply
# emit the relevant signal [2]
# [1] https://github.com/magmax/python-readchar/issues/40
# [2] https://github.com/magmax/python-readchar/issues/40#issuecomment-934018349
if character == '\x03':
# CTRL-C
os.kill(os.getpid(), signal.SIGINT)
if character == '\x1a':
# CTRL-Z
os.kill(os.getpid(), signal.SIGTSTP)
You can find readchar's list of character codes here.
I agree the best solution is to modify the library and add an option. But this workaround is sufficient for my purposes right now.
I suspect that while some people want to intercept ctrl+c and ctrl+z, many also do not want to. I am no expert in this area so there may be significant issues with this but I figured I'd document the issue and share what I put together (along with another
#python
user). This was written as a thing to usereadchar
but presumably could be translated into a feature forreadchar
proper. No promises right now that I will get around to that.https://repl.it/@altendky/readchar-getch-6