-1

htop description here. In questions about getch, I saw statements like "console is different under every system". But JVM implementation is also different under every system.
Well, the comments below show that my question doesn't represent the simplicity of the problem. Some related questions are:

As for me, they are, in part, derivatives of the lack of C conio.h like functionality. Some of the questions address much more complex tasks than reading ➡ key. And there some 3rd party libraries for solving them jline3, jCurses. But besides they are 3rd party and solving their own complex tasks, they have to deal with the specific terminal settings, JNI, or JNA because of the original problem. So, what if the task is jast to read a letter or arrow button after it had been typed?
Outside Java world, there projects like GNU Readline, curses. But unlike pure-Java projects, they can't benefit from "run anywhere" VM. Maybe some SIM-cards don't have terminal capabilities. On the other hand, AWT, which needs much more hardware capabilities than terminal, was implemented. Cause it was considered worthy, I guess. And nowadays more and more SoCs are capable of X11, not even terminal. So, are full-fledged terminals so uncommon, and their functionality implementation on various systems so diverse, that instruments for its straightforward implementation have no place in Java SE?

user9999
  • 113
  • 2
  • 10
  • 2
    What do you mean? Java certainly has APIs to get a character from standard input. It might not be _named_ getch. – Louis Wasserman Dec 04 '20 at 19:02
  • 2
    `conio.h` is a DOS specific header, not part of C or POSIX, and some of the functionality fundamentally can't be implemented on other OS. Java does not aim to let you fully utilize the available functionality on each platform (though it does allow it via native libraries), but instead to provide a common, portable interface that works on all platforms. – that other guy Dec 04 '20 at 19:12
  • 1
    _"But JVM is also different under every system."_ No it isn't, that is why it's a VM (virtual machine). – Mark Rotteveel Dec 04 '20 at 19:35
  • @Louis Wasserman It gets a string, not a character, only after terminal sends it. – user9999 Dec 04 '20 at 19:50
  • @MarkRotteveel at list 6 different here https://www.oracle.com/java/technologies/javase-jre8-downloads.html – user9999 Dec 04 '20 at 19:56
  • 1
    That is the software needed to run the virtual machine. That is tailored to the platform, the end result is the Java Virtual Machine that runs your Java program, and that virtual machine is the same independent of the underlying platform. – Mark Rotteveel Dec 04 '20 at 20:00
  • @MarkRotteveel Maybe I've used wrong terminology. But I assume that there are different implementations of JRE that provide identical functionality. And that functionality doesn't give a simple solution for for example "TAB autocompletion" without usage of awt. Maybe I should change the question. – user9999 Dec 04 '20 at 20:38
  • Please don't use AWT. You should be using JavaFX or, if you have to, Swing. – NomadMaker Dec 04 '20 at 21:09
  • @user9999: Which API are you referring to that "gets a string, not a character"? Are you sure there isn't another API that does what you're looking for? – Louis Wasserman Dec 04 '20 at 21:21
  • @LouisWasserman I mean any API that could return a single character or keyCode without pressing Enter. The 2nd part of this answer may illustrate the problem. https://stackoverflow.com/questions/1864076/equivalent-function-to-cs-getch-in-java/1879620#1879620 – user9999 Dec 04 '20 at 21:34
  • 1
    It's not really an answer but some solutions could likely be found by studying this project that handles single key presses in Java: https://github.com/mabe02/lanterna . It's a curses-like library written in Java that deals with console UIs. – Quintesse Feb 10 '22 at 12:29
  • Why people can develop products with this crap https://hyper.is/, but can't do this with the #1 compiled language with a solid pack of desktop components? Facepalm – user9999 Mar 30 '22 at 19:25

1 Answers1

1

On Linux, try

var inputStream = new FileInputStream( "/dev/tty" );
while( true )
{
    var c = inputStream.read();
    // Process c here …
    System.out.print( (char) c );
}

Have fun! Because c will hold also data you have never entered. The loop is permanently reading values from the tty, not only what you type …

You may need some additional configuration settings for the tty, depending on your operating system flavour.

But this is (on the console) the closest you can get to getch in Java.

Oh, and regarding "full fledged terminal": most terminals I know will send data only on pressing Enter or alike, at least in their default setup. What you are talking about is something that exists in that form only on PCs and alike where the keyboard and the screen are parts of the executing machine.

tquadrat
  • 3,033
  • 1
  • 16
  • 29
  • You a right about I simply used PC as a reference, but by full-fledged terminal I mean that both GitBash and new Windows Terminal, via ssh, display colorful htop from both virtual machines on Digital Ocean and RaspberryPi Zero W, with F-key functionality. – user9999 Dec 05 '20 at 20:57
  • The actual setUp is new Windows Terminal->ssh-> Rasberry Pi Zero W -> Rasberry OS Lite – user9999 Dec 05 '20 at 20:58
  • @tquadrad Without any settings your code works but blocks. With stty -icanon min 1 and stty -echo it works nice. I'm not sure about codes of F-keys and backspace does nothing, but vertical arrows actually move the cursor, so it far way close to "htop". It works the same way with System.in, so I tried it in GitBash on Windows. Immediate input works, but arrows don't. If connect a keyboard and HDMI directly to RaspberryPi it works the same way as ssh. – user9999 Dec 05 '20 at 20:59
  • @user9999: with a "full fledged terminal", I meant a *hardware terminal*, not a terminal software program, like telnet, ssh, or alike. And in this post I used "alike" definitely too often … – tquadrat Dec 06 '20 at 07:58