OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Can Python's input be terminated with another key?

  • Thread starter Thread starter mRcSchwering
  • Start date Start date
M

mRcSchwering

Guest
I am writing an interactive CLI and just thought about giving the user a different way of confirming an input. So, usually I would use python's input(). With that a user would submit input by pressing Enter.

Would it be possible to let a user submit input by pressing e.g. Tab or Ctrl+D? I wanted to make it possible to quickly use the CLI with the left hand only (Enter is far away).

I tried to work around with Ctrl+D by catching EOFError

Code:
def _input(msg: str) -> str:
    try:
        inpt = input(msg)
    except EOFError:
        pass
    return inpt

but of course that doesn't work because the exception prevents user input from being read. sys.stdin and fileinput.input also rely on Enter.
<p>I am writing an interactive CLI and just thought about giving the user a different way of confirming an input.
So, usually I would use python's <code>input()</code>.
With that a user would submit input by pressing <kbd>Enter</kbd>.</p>
<p>Would it be possible to let a user submit input by pressing e.g. <kbd>Tab</kbd> or <kbd>Ctrl+D</kbd>?
I wanted to make it possible to quickly use the CLI with the left hand only (<kbd>Enter</kbd> is far away).</p>
<p>I tried to work around with <kbd>Ctrl+D</kbd> by catching <code>EOFError</code></p>
<pre class="lang-py prettyprint-override"><code>def _input(msg: str) -> str:
try:
inpt = input(msg)
except EOFError:
pass
return inpt
</code></pre>
<p>but of course that doesn't work because the exception prevents user input from being read.
<code>sys.stdin</code> and <code>fileinput.input</code> also rely on <kbd>Enter</kbd>.</p>
 

Latest posts

Online statistics

Members online
1
Guests online
2
Total visitors
3
Top