Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable the arrow keys while a JAR is executed

Tags:

bash

sh

jar

The case is as follows:

1) I have a script somescript.sh.

2) At the end of the script, there is an eval statement which triggers the execution of a JAR.

3) When the JAR is executed, it will ask the user to provide an a/b/c option as a possible answer - important fact is that this questions come from the JAR application, so the console "logic" regarding the questions is written in Java and not placed in the script itself.

The problem: while executing the JAR program, the user can press the arrow keys and this will result in an ugly outcome such as ^[[A ^[[B ^[[C ^[[D.

This thread clarifies the ugly outcome: Why the terminal shows "^[[A" "^[[B" "^[[C" "^[[D" when pressing the arrow keys in Ubuntu?

Question: How is it possible to disable the arrow keys while the JAR is executed?

like image 886
Insanovation Avatar asked Jun 29 '17 15:06

Insanovation


People also ask

Can I disable my arrow keys?

Tap 'Layout & keys' Check or uncheck 'Arrow Keys'

How do I turn off the arrow keys on my HP laptop?

I understand that you would like to disable arrow keys controlling the cursor. Press Windows + R, type “Mouse keys” in the dialogue box and open the setting of 'Turn Mouse keys on or off'. Now, uncheck the option of Control your mouse with a keypad. Save changes and exit. ...


1 Answers

Now this is rather convoluted, but it`s still better than nothing.

The code below is a primitive BASH key filter to bypass cursor keys and halt any input on pressing Enter. Currently it lacks backspace support, but this is also doable if need be.

Requires BASH 4.2 or greater.

while true; do
    read -s -N1          c1
    read -s -N2 -t 0.001 c2
    read -s -N1 -t 0.001 c3
    case "$c1$c2$c3" in
        $'\x0a' | $'') echo "";
                       break ;; # Enter
        $'\x1b\x5b\x41')     ;; # Up arrow
        $'\x1b\x5b\x42')     ;; # Down arrow
        $'\x1b\x5b\x43')     ;; # Right arrow
        $'\x1b\x5b\x44')     ;; # Left arrow
        *) echo -n $c1$c2$c3 ;; # [guaranteed to be non-empty]
    esac
done | tee >(stdbuf -o0 java -jar your_applet.jar)
  • read is a BASH built-in function that captures keyboard input.
  • echo is a BASH built-in function that prints what it`s told.
  • tee redirects the output to both STDOUT and the file provided.
  • >() is the so-called process substitution; it acts like a file for the writer and passes the resulting file`s contents as input to the command inside its brackets.
  • stdbuf disables output buffering, just in case.

Both tee and stdbuf are parts of coreutils, so they have to be present almost everywhere (maybe except Android, but that`s another story).

[UPD.] Last line, adapted:

done | tee >(stdbuf -o0 "$JAVA" $JAVA_OPTS -jar "$JBOSS_HOME/jboss-modules.jar" -mp "$JBOSS_MODULEPATH" org.jboss.as.domain-add-user "$@")

Hopefully that`d work.

[UPD2.] FINALLY! A different solution!

Just add this before eval:

stty=$(stty -g)
stty -ctlecho
trap "stty $stty" HUP INT QUIT PIPE TERM

Okay, this is also not perfect (the user is now able to travel the screen by pressing cursor keys…) but that`s still better than ^[[B^[[A^[[D^[[C

like image 176
hidefromkgb Avatar answered Sep 21 '22 06:09

hidefromkgb