javaterminalraspbianawtrobotsystem.out

why is newline(\n) behaving differently then pressing enter on Raspbian CLI


I made a application that basically reads out data and prints it out on the raspbian Command Line Interface (CLI) using system.out.print. Printing text on the CLI using the mentioned method works regarding printing text.

After printing the text I want it also to behave like pressing enter button (submitting the data). Imagine you are prompted by the CLI to enter a value, the application prints the data and then I want the application to behave as if a user would press enter.

The application is a daemon process which prints data from an external device in the CLI. It could and does print this data at any time this is intentional. Sending the data to a specific application or a set of applications is not wanted because the application commanding the prompt could swap every x amount of time. With the newline command the cursor goes to the next line but it does not behave as if a user were to press the enter button of the keyboard.

I tried using \n which does move the cursor to the next line but it does not submit the data. In the end I want the data to be entered automatically without the user pressing the enter button manually.

After that I tried out awt.robot class but that doesn't work because it throws a headless exception and after googling a bit I believe it's related to a GUI or functionality which won't be installed and used on the Raspbian.

I've also found people mentioning JNA and JNI libraries but I can't find any example (at least not for linux devices) to simulate a enter press by the user.

Here is a more concrete example. The CLI prompts:

Weight:

The daemon process prints 0.233. So the CLI will look like:

Weight: 0.233

Then the application must behave as if a user presses enter. Using the newline character only moves the cursor and does not behave as if a user presses the enter button like:

Weight: 0.233
_ (representing cursor).

Smallest/simplest reproducible example:

public static void main(String[] args) {
  while(true) {
    //this moves the cursor to the next line and doesn't enter the line in a prompt.
    System.out.println("abc");
    //have to time that it goes into a prompt. I have a startup login prompt which I can test it with.
    //or whatever suits your needs
    Thread.sleep(20000);
  }
}

I'm using java 8 and the Raspbian version is 8 and the raspberry version is 3.

Hopefully anyone knows what to do or what I'm missing. Any help is welcome.


Solution

  • After much experimenting I found out that the that's just the way the TTY interprets the /n nowadays. I had an older Linux installation which emulates a /n as a keyboard press but that doesn't happen in that raspbian version.

    So basically I had to write directly to the correct IO channel same way as a keyboard does. The you cannot emulate a keyboard enter press directly to the terminal. Only by either writing directly to the correct IO channel or using a keyboard emulator.