1

I am saving text in a DB with a Console application on Windows and have the prompt to enter text

entry=(input('Enter note: '))

I want it to support newlines, and therefore make the end entry Ctrl+Enter instead of just Return. Is there a way to do this in the console?

The suggested question does not answer how to end text entry with Ctrl+Enter. Instead it only provides Ctrl+Z as how to end text input

OrigamiEye
  • 864
  • 1
  • 12
  • 31
  • 1
    check `msvcrt` https://docs.python.org/3/library/msvcrt.html – Marcin Orlowski Apr 25 '23 at 06:40
  • 2
    Does this answer your question? [Python 3: receive user input including newline characters](https://stackoverflow.com/questions/2542171/python-3-receive-user-input-including-newline-characters) – Nick Apr 25 '23 at 06:42
  • 1
    See also https://stackoverflow.com/questions/29454365/what-does-sys-stdin-read – Nick Apr 25 '23 at 06:42
  • @MarcinOrlowski can you specify how mean the msvrt library can be used here? – OrigamiEye Apr 25 '23 at 11:17

2 Answers2

1

This doesn't exactly answer your question and is only meant as an alternative way of accomplishing what you seem to want. The only reason I am posting this as an "answer" is so I can show a code block. I think the 'Enter' key is going to terminate input() long before you get to typing 'Ctrl-Enter'.

entry=(input('Enter note - Use | to separate lines: '))
print(entry)
entry = entry.replace('|','\n')
print(entry)
Pragmatic_Lee
  • 473
  • 1
  • 4
  • 10
  • 1
    Thanks I've also posted the alternative I'm using until I find how to get the Ctrl+Enter behavoir to work – OrigamiEye Apr 25 '23 at 13:04
0

I don't think there is an answer to changing the behavior of the CLI. My alternative is to use sys.stdin and q to quit text entry

  entryMode=True
    while entryMode:
        print("Enter Note: (q to end entry: qq to leave entry mode : qqq to exit)")
        entryLines=[]
        for line in sys.stdin:
            if 'q' == line.rstrip():
                break
            if 'qq' == line.rstrip():
                entryMode=False
                break
            if 'qqq' == line.rstrip():
                entryMode=False
                exit()
            entryLines.append(line)
        entry = "".join(entryLines)
OrigamiEye
  • 864
  • 1
  • 12
  • 31