Arrow Keys

263 views
Skip to first unread message

Andrew G

unread,
Aug 9, 2020, 11:11:53 PM8/9/20
to jline-users
Hey guys,

I was hoping someone has run into this before, I must be doing something wrong/missing something.

To start, I'm using these:

        <dependency>
            <groupId>org.jline</groupId>
            <artifactId>jline</artifactId>
            <version>3.16.0</version>
        </dependency>
        <dependency>
            <groupId>org.jline</groupId>
            <artifactId>jline-terminal-jna</artifactId>
            <version>3.16.0</version>
        </dependency>

I'm simply trying to detect when an up/down key is pressed, my test is:

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.cli.Option;
import org.jline.builtins.Less;
import org.jline.builtins.Source;
import org.jline.keymap.BindingReader;
import org.jline.keymap.KeyMap;
import org.jline.terminal.Terminal;
import org.jline.terminal.TerminalBuilder;
import org.jline.utils.InfoCmp.Capability;

public class TestArrowKeys {

    public static void main(String[] argsthrows IOExceptionInterruptedException {
        try (Terminal terminal = TerminalBuilder
            .builder()
            .jna(true)
            .build()) {

            // this works
            /*
            Less less = new Less(terminal, new File(".").getAbsoluteFile().toPath());
            List<Source> sources = new ArrayList<>();
            sources.add(new Source.PathSource(new File("pom.xml"), "pom.xml"));
            less.run(sources);
            */

            // this doesn't
            BindingReader bindingReader = new BindingReader(terminal.reader());

            KeyMap<OperationkeyMap = new KeyMap<>();
            keyMap.bind(Operation.UPkey(terminalCapability.key_up));
            keyMap.bind(Operation.DOWNkey(terminalCapability.key_down));
            keyMap.bind(Operation.COMMANDArrays.asList("a""b""c""d"));

            terminal.writer().flush();

            while (true) {
                Operation op = bindingReader.readBinding(keyMap);
                System.out.println("read op: " + op);
            }
        }
    }
    
    public enum Operation {
        UP,
        DOWN,
        COMMAND
    }
}


With the code as is, output looks like, notice the ^[[B^[[A are arrow key presses:

f
s
a
read op: COMMAND
^[[B^[[A^[[B^[[A

a
read op: COMMAND

but if I uncomment the 'less' example, arrow keys seem to work fine.

I'm testing this in an WSL 2 - ubuntu xterm:

uname -a
Linux Andrew-PC 4.19.104-microsoft-standard #1 SMP Wed Feb 19 06:37:35 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

Any idea what I'm missing?

Thank you,
Andrew

Guillaume Nodet

unread,
Aug 10, 2020, 4:12:05 AM8/10/20
to jline...@googlegroups.com
I think you need to initialize the terminal correctly:
Attributes attr = terminal.enterRawMode();
terminal.puts(Capability.enter_ca_mode);
terminal.puts(Capability.keypad_xmit);

Cheers,
Guillaume

--
You received this message because you are subscribed to the Google Groups "jline-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jline-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jline-users/5dabd2fe-fe84-459d-947c-a9f69a752f2eo%40googlegroups.com.

Andrew G

unread,
Aug 10, 2020, 9:29:59 AM8/10/20
to jline-users
That worked thank you very much!

I had tried the terminal.puts calls by themselves, but not with the terminal.enterRawMode.

Guillaume

To unsubscribe from this group and stop receiving emails from it, send an email to jline...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages