-1

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.

denbrau
  • 11
  • 4
  • "After printing the text I want it also to behave like pressing enter (submitting the data)" submitting it to what? Your own application's input? Another application? How are you trying to do this? Please [edit] your question to include more detail. – Federico klez Culloca Dec 30 '20 at 15:01
  • I edited the question, hopefully it's more clear now @FedericoklezCulloca. – denbrau Dec 30 '20 at 15:13
  • @denbrau No, it's not, sorry :) please explicitly answer my question. Do you want your own application to print its own inputs? Because if this is what you're trying to do it doesn't make any sense. Just print the string and then fill your variables directly. If you're trying to fill the inputs of another application look into input/output redirection in linux (at which point the fact that your app is written in java becomes irrelevant) – Federico klez Culloca Dec 30 '20 at 15:16
  • When you print text, all you're doing is printing it. The linux shell isn't seeing that data as a command. You need to create a new process to access the shell. – NomadMaker Dec 30 '20 at 15:16
  • Output is not input. That's really all there is to it. When you press keys, the characters are prepared for input and are also displayed. It does not follow that displaying characters also prepare them for input. – a guest Dec 30 '20 at 15:17
  • @FedericoklezCulloca. I added more detail. I do not want to specifically target an application or range of applications. my application just has to print the text in the CLI during a different applications command prompt and behave as if a user presses enter. – denbrau Dec 30 '20 at 15:47
  • Sorry, I still don't understand. Are you trying to launch terminal commands? Can you please give an **explicit** example, with actual text, and actual expected results? – Federico klez Culloca Dec 30 '20 at 15:50
  • @FedericoklezCulloca I added a concrete example of what happens. – denbrau Dec 30 '20 at 16:04
  • Fine, but how does this work? Are the daemon and the application the same process? Does one launch the other? How? Can you please show some relevant code? – Federico klez Culloca Dec 30 '20 at 16:06
  • Please provide a [mre]. – Yunnosch Dec 31 '20 at 08:25
  • @FedericoklezCulloca added a reproducible sample in code. Do mind this is executed on a raspbian with only a command line interface. – denbrau Dec 31 '20 at 10:55

1 Answers1

0

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.

denbrau
  • 11
  • 4
  • Yes you can. I've written software that does exactly this. You mind find it useful to look into [How are \n and \r handled differently on Linux and Windows?](https://superuser.com/q/374028/763152). It will nudge you in the right direction. Also it's not `/n` but `\n`. – pfabri Feb 12 '21 at 11:14
  • What language you wrote it in? In java I checked multiple versions as well as the OpenJDK and Oracle versions. They both need a non headless environment for keyboard emulation for example. And no that article is not helpful, it only verifies that it doesn't work in the specific scenario of the question. – denbrau Feb 15 '21 at 07:48
  • In a dumbed-down version of C++ on a microcontroller. But now that I think of it, I was doing something slightly different. The microcontroller accepted typed commands from the computer it was connected to, which it stored internally (as setup data for a measurement) then transformed and *echoed back to the computer*. The returned data was interpreted by `screen` on Linux, which is a terminal multiplexer. So the emulation took place on the computer itself. Apologies, I misread your problem. – pfabri Feb 15 '21 at 09:19