Alternate ways to stop print() in python - python

I have to get user input from the console. Using python input() statement only stops and returns the input when the user clicks "ENTER". But I want it to also happen with "TAB".
Is there a way that python input() can respond to both "ENTER" and "TAB" and return the user input?
I have tried different modules like msvcrt, keyboard, pynput, etc., But they simply read characters one by one. I had to deal with the below items:
Shift + ch (for uppercase)
Inserting characters in between (using right/left arrows)
Copy and Paste
Insert, etc
So I don't want to manually code for all these and many unknowns may raise in the future. These are all already taken care by python input(). I just need to stop it and return the output for "TAB" as well.
Please let me know if there is a way to achieve this.
It might be helpful to a lot of users.

Related

Renpy pasteable input

A game I'm working on needs an input in which a special code can be pasted. The code is a random hash of 24 characters, which would be hard for players to write manually. I haven't found any good quality ways of implementing such an input.
Could this be even done? And if so, how?
I'm just started with Renpy not so long ago and my knowledge is currently scarce
It is mentioned in the documentation.
Input
Creates a text input area, which allows the user to enter text. When > the user presses return, the text will be returned by the interaction.
copypaste
If True, it becomes possible to copy and paste into this input. (By
default, disabled.)
Just set copypaste=True

How to trigger a "undo" for user input

I have the following user input statements that are part of a larger function. When prompted for user input it is the case that a user might fat finger the keyboard and continue with the following input prompts. The user input is arbitrary and it really depends on them if they plug in the correct information. However, it could be neat to go back to the previous input statement and continue from there by triggering a keybind (E.g crtl + b lets say) or something similar.
I understand that this could be refined by having explicit invalid inputs with a conditional statement. however, this wouldn't be the use case for my situation. I'm not sure how to go about this. Hoping someone can point me in the right direction.
whichDIMM = input("\nWhich DIMM needs to be replaced?: (ex. A4) \n")
dimmSize = input("\n64G/32G/16G memory?\n")
hostname = input("\nEnter hostname: \n"
Would it be possible to 'Undo' and go back to the previous prompt in case there are spelling mistakes? then continue as expected? otherwise, the user will need to re-run the entire program.
For example:
Which DIMM needs to be replaced?
*mistypes*
64G/32G/16G memory?
(trigger undo)
Which DIMM needs to be replaced?
DIMM_A4
64G/32G/16G memory?
64G
Enter hostname:
myhostname
...
continue with the rest of the script
You are basically trying to turn this into a form where you can move between fields and then submit the whole form. This has been written in many ways already so you could just use one such library. A simple one would be npyscreen for example.

Keeping the user's input intact when outputing to terminal at the same time

To simplify, let's say I'm trying to write a command line two-way chat in Python. I would like the user to input his message with input() at the command prompt, but a listening thread could print a message at any moment. By default, this would "break" the user's input. Visually something like this:
userB>Stop interuserA wrote:Hey check this out!
rupting me!
The closest I was able to find was this answer here which is almost, but not exactly, what I'm looking for, but it did point me towards the blessings package which seems to be what I need (although I'm happy with an answer for any package, or even pure ANSII).
What I'm trying to achieve is to print incoming output from a Thread above the user's input, so that his text doesn't break. Let's say the user is typing:
userB>Stop inter
Suddenly a message comes in from the thread, but our user's input doesn't brake:
userA says: Ok I won't interrupt you
userB>Stop inter
What should my threads theoretical print_incoming_message() method look like to achieve this?
NB: I'm using Linux and am not interested in cross-platform compatibility.
There are two ways of doing this.
One is to use ncurses. There are python bindings for this. With ncurses, the terminal screen is under your complete control, and you can print characters at any point.
Without ncurses, you can't write above the current line. What you can do, however, is print a \r character and go back to the beginning of the line.
If you save the user's input (say he wrote foo), and you want to print the line bar above that, you can output:
\rbar\nfoo
This will overwrite the current line, and introduce a newline, moving the user's input down. The effect is similar, but it won't be as tamper-proof as ncurses.

how do I keep my input at the same screen location?

My current input method is a for loop. I input an integer it gets added to a list then it goes around to the next input. This produces a new line each time it goes around. I would like to keep the input entries at the same location. I'm new to Python and programming in general so I have no clue where to start. My input statement is in this form:
var= input(" message: ")
Thank You
If you are working with UNIX or Windows terminals, you can add an ASCI escape ("\033[A") to move the cursor one line up to ask for input again, as shown in the example here, so in this way, you can get your input message at the same position by "overwriting" the previous input message.
tl;dr: there's a reason most simple applications don't do this: it's a lot of work for not very much payoff.
If you're talking about making sure that the "message: " line always appears in the same place in the window, then you can't do it with plain input: it doesn't take an "end of line" argument and actually the newline happens before input gets a hold of the input string. (I.e. when the user hits RETURN.)
You can do it, especially if you don't care about Windows, using getch mode, which basically says that the terminal passes characters to you, not whole lines, and you need to decide how to interpret them. The getch package seems like it might be able to help here, it even supports Windows!
Another option (probably not as effective or a nuclear bomb when what you need is a scalpel) would be to use something like curses or blessings' terminal.location abilities. And blessings, I believe, incorporates ANSI escape chars. (As mentioned in the other answer to this question, it's probably the right way to go.)

how to ask user input a string with a timeout embeded in python on windows machine?

want to ask user to input something but not want to wait forever. There is a solution for Linux, Keyboard input with timeout in Python, but I am in windows environment. anybody can help me?
Credit to Alex Martelli
Unfortunately, on Windows,
select.select works only on sockets,
not ordinary files nor the console.
So, if you want to run on Windows, you
need a different approach. On Windows
only, the Python standard library has
a small module named msvcrt, including
functions such as msvcrt.kbhit which
tells you whether any keystroke is
waiting to be read. Here, you might
sys.stdout.write the prompt, then
enter a small loop (including a
time.sleep(0.2) or so) which waits to
see whether the user is pressing any
key -- if so then you can
sys.stdin.readline etc, but if after
your desired timeout is over no key
has been hit, then just return the
empty string from your function.
All of this assumes that if the user
has STARTED typing something then you
want to wait indefinitely (not timeout
in the middle of their entering their
answer!). Otherwise, you have more
work to do, since you must ensure that
the user has hit a Return (which means
you must peek at exactly what's in
sys.stdin, resp. use msvcrt.getch, one
character at a time). Fortunately, the
slightly simpler approach of waiting
indefinitely if the user has started
entering seems to be the preferable
one from a user interface viewpoint --
it lets you deal with unattended
consoles as you desire, yet IF the
user is around at all it gives the
user all the time they want to
COMPLETE their answer.

Categories

Resources