I'm getting Arrow Key values in my Python Input using input()
.
This only happens during the time of execution of a Python Script. It doesn't happen if Input is taken from the Interpreter.
The Arrow Key values I'm referring to: Why does the terminal show "^[[A" "^[[B" "^[[C" "^[[D" when pressing the arrow keys in Ubuntu?
Contents of Script File:
s = input("Enter Something: ")
print(s)
Terminal Output:
$ python input_example.py
Enter Something: Now Pressing Left Arrow Key^[[D^[[D^[[D^[[D
Now Pressing Left Arrow Key
I'm unable to navigate (or say change cursor position) left or right while writing input cause the Arrow Key Values Show up in the Input. Is there any way to avoid them? In Terminal, generally, one can change the cursor position, this problem doesn't happen unlike Python's input()
.
P.s. I don't want to change any settings in bash cause I'm trying to write a script that works in all consoles. I'm a noob so I don't understand many things. I hope this community can help me out.
To exit the input mode, you have to use Ctrl + Z which appears in the above shell snippet as ^Z .
K_UP , K_DOWN , K_LEFT , and K_RIGHT correspond to the arrow keys on the keyboard. If the dictionary entry for that key is True , then that key is down, and you move the player .
Then we call e.preventDefault to stop the arrow keys from changing the input value. To prevent arrow keys from changing values in a number input with JavaScript, we can check if the arrow keys are pressed and call preventDefault is they are.
on_key_release (): This function will be called whenever a keyboard key is released. Inside this function, we will check if the key released is the up arrow key then we will print “upper arrow key is released”. Below is the implementation:
If you a using a GUI/event-driven framework, then yes, it's possible. If you are using console python, then no. Here is an example of a Tkinter key logger ...
In order to start the arrow keys, use: Using input will not work. You must use curses.initscr ().getch (). That's long, right? Anyways, once you have done that, you have the key codes. For the up arrow key, do curses.KEY_UP. For the down arrow key, use curses.KEY_DOWN. I think you know what the other 2 arrow key codes are.
Found a way to prevent this! You just have to import the readline module
import readline
This will make the standard input() method utilize some of its utilities, enabling normal arrow-key usage and more.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With