I've read answers like in:
They explained how to look for a sentinel value, or how to catch OSError
, but those are not what I'm looking for. I'd like to let users insert texts just like how they type messages in Telegram, Whatsapp and etc. but in terminal!
As long as they hit shift + enter, they should be able to continue writing on the next line and when they hit enter, the input should be sent.
I guess if I can somehow insert an arbitrary character(like "#\n"
) to the standard input when a user presses shift + enter, I would detect and break out of the below loop:
import sys
while True:
line = sys.stdin.readline()
if line[-2:] == "#\n":
break
print(bytes(line, encoding="ascii"))
I found this similar question but its answers don't work for me.
I want it for unix-like system's terminal.