Problem with arrow keys in shell with python and java

About programming and getting involved with Linux Mint development
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
Qwerty
Level 1
Level 1
Posts: 2
Joined: Mon Aug 28, 2023 7:58 pm

Problem with arrow keys in shell with python and java

Post by Qwerty »

Hello, I have a problem when I run scripts in Python and Java. When I press the arrow keys, they input ASCII characters or something similar instead of moving in the shell.

Code: Select all

~/Escritorio/programacion$ java Prueba
Escribe algo
^[[D^[[C^[[D^[[C^[[B^[[B^[[B^[[A^[[A^[[A^[[C^[[D^[[B^[[D^[[A^[[C^[[D^[[B^[[A^[[C^[[D^[[B^[[A^[[C^[[B^[[D^[[C^[[A^[[D^[[B^[[D^[[A^[[B^[[C^[[D^[[B^[[A^[[C^[[D^[[B^[[C^[[D^[[B^[[D^[[A^[[C^[[B^[[D^[[A^[[B^[[C^[[D^[[A^[[B^[[C^C

Code: Select all

python3 prueba.py 
Escribe algo         Hello this is an example^[[D^[[D^[[D^[[D^[[D^[[D^[[C^[[B^[[D^[[A^[[C^[[B^[[D^[[A^[[C^[[D^[[A^[[B^[[D^[[C^[[A^[[D^[[B^[[D^[[D^[[C^[[B^[[D^[[A^[[C^[[D^[[B^[[C^[[A^[[D^[[B^[[C^[[B^[[D^[[A^[[C^CTraceback (most recent call last):
  File "/home/lagt/Escritorio/programacion/prueba.py", line 1, in <module>
    a = input('Escribe algo')
KeyboardInterrupt

Last edited by LockBot on Fri Mar 01, 2024 11:00 pm, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
dave0808
Level 5
Level 5
Posts: 987
Joined: Sat May 16, 2015 1:02 pm

Re: Problem with arrow keys in shell with python and java

Post by dave0808 »

The arrow keys work within the shell because that's part of the shell's input interpreter.

When you are running your program in Java or Python and are reading from stdin, it is your program that is accepting the input and therefore your program that needs to do something with that input.

I don't do Python, but in Java, the usual way for reading from stdin is to read input from System.in, which is an implementation of java.io.InputStream and won't provide you with "control codes". Equally with java.io.Console, which uses java.io.Reader which itself is a character based class.

Looking on stackoverflow there doesn't seem to be a definitive answer to this. You will probably need to put the console into a raw mode and somehow get hold of the raw key presses.
User avatar
spamegg
Level 14
Level 14
Posts: 5117
Joined: Mon Oct 28, 2019 2:34 am
Contact:

Re: Problem with arrow keys in shell with python and java

Post by spamegg »

Install the rlwrap package and run your scripts / commands with rlwrap in front.
jacobmallll
Level 1
Level 1
Posts: 6
Joined: Thu Dec 14, 2023 5:07 am
Location: USA
Contact:

Re: Problem with arrow keys in shell with python and java

Post by jacobmallll »

Hi,

If you're using Scanner or BufferedReader for input in Java, you might want to consider enabling raw mode for your terminal.

Follow this-

Code: Select all

import java.io.Console;

public class Prueba {
    public static void main(String[] args) {
        Console console = System.console();
        if (console != null) {
            console.printf("Escribe algo: ");
            String input = console.readLine();
            System.out.println("You entered: " + input);
        }
    }
}
It will work for you.
Thanks
Jacob Mall
dave0808
Level 5
Level 5
Posts: 987
Joined: Sat May 16, 2015 1:02 pm

Re: Problem with arrow keys in shell with python and java

Post by dave0808 »

jacobmallll wrote: Fri Dec 15, 2023 2:35 am It will work for you.
How, exactly, is this code going to do what the OP wants? As I previously stated, the Console class is a string based reader and as the OP has already discovered, this will just write control characters as the arrow keys are pressed.
dave0808
Level 5
Level 5
Posts: 987
Joined: Sat May 16, 2015 1:02 pm

Re: Problem with arrow keys in shell with python and java

Post by dave0808 »

BTW, KeyListener is for Java UI applications (AWT, Swing) and does not work for the command line that the OP was asking about.
Locked

Return to “Programming & Development”