Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Avoid Arrow Key Values in Python Input?

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.

like image 334
Gagan Deep Singh Avatar asked Apr 04 '20 09:04

Gagan Deep Singh


People also ask

How do you avoid input in python?

To exit the input mode, you have to use Ctrl + Z which appears in the above shell snippet as ^Z .

What are the arrow keys in Python?

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 .

How to prevent arrow keys from changing values in number input?

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.

How to print “Upper Arrow Key is released” in Python?

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:

Is it possible to log keystrokes in Python?

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 ...

How do I start the arrow keys on the keyboard?

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.


1 Answers

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.

like image 190
Julian K Avatar answered Oct 13 '22 05:10

Julian K