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

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.

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.

Related

Java println multithreading and stdin question

i have two threads: one of them (server) is printing something to the console and the other (user) one is taking input from console. The problem occurs if the first thread prints something while user is typing into console. it looks like for example
command from usermessage from server
or if the user hasn't typed the command fully
commmessage from server
and it messes up the command from the user
is there a way to make it look like
message from server
command from user
or
message from server
comm
so to print one line above the currently typed text in the console and maintain the user input. i still want the first thread to print while the user is typing so locking this thread untill user enters the command isn't an option
[I want] to print one line above the currently typed text in the console and maintain the user input.
There are no easy answers here unfortunately. The user input being entered is actually being echoed by the terminal and not by the Java program. You have no control over the input and can't reprint it once the standard output is displayed.
The only way to do this otherwise is to turn off terminal echoing and control the user keystrokes in your program, but that makes it a lot more complicated. See: How to disable console echoing
You could also use some sort of Java dialog box and then pop the standard out message in another dialog or let it go to standard out.
i still want the first thread to print while the user is typing so locking this thread until user enters the command isn't an option
Yeah I fear that locking out the standard output messages is only way for you to do this unless you want to use a different mechanism to get your user input.

How to make PrintWriter write in log-in screen or in any text box I choose in windows 10

I am writing a java program to print password in log-in page of windows 10 (to get automatically login when I do some action)
I try to use printwriter to do that .
I managed to print the text inside a text file .
but I want to print the text in password's text box of login page in windows 10
any idea please ..
Thanks
import java.io.*;
class Hasan{
public static void main(String[] args)
try {
PrintWriter writer=PrintWriter(System.out);
writer.write("mypassword");
}
catch (Exception e) {
System.out.println(e);
}
}
}
when I used System.out I just could printed text in console screen
That's not going to work. While you can use for example java.awt.Robot to fill out things when the desktop is unlocked and your program is running, the login screen is a whole different thing. For security reasons you won't be able to get any custom programs to do the logging in for you. At least not with Java, and not without root access.
PrintWriter is designed to print a sequence of bytes on a stream. UI widgets, like textboxes, have no stream support.
If you want to target a textbox owned by a process in the same security context as yours, you have to identify the process owning the window and use the OS primitives (SendMessage, PostMessage, ...) to enter your text.
Processes outside your security context are unreachable and at least require that your process is elevated.
Note that certain textboxes may refuse certain types of message when they are working in ''password'' mode, i.e. masking the input. This is to prevent applications from stealing the context of the textbox.
A more wise solution, would be to let the user set the focus on the UI control they wants, and then let them pressing a keyboard hotkey to trigger your program. Your program can then use use SendInput to send the keyboard characters one by one as if the user was typing.
Both the solutions are feasable in native language (C, C++) or PInvoked (C#), for java, have a look at Sending a Keyboard Input with Java JNA and SendInput().

How to grab CMD input with line that updates

I'm sure some people are familiar with the concept. You run a command at the CLI and as it progresses, the one line in front of you updates with a percentage. Under the bonnet, before each line is printed, it clears the current line, so instead of 100 lines of progress updates, you just have one line which updates until it hits 100%.
I want to make an app that will echo this information out into an embeded diaplay window. I'm fairly new to Java and I'd like my app to run a Windows command (sfc /scannow to be exact), display the output to the user on-the-fly and then once complete, I want to analyse the log file and give a readable conclusion to the user.
Is there some kind of library or special way of going about doing this so that when the line is "updated" in the console window, I can simply overwrite the current line in my display window?
The line is probably updated by the program writing \r control character (carriage return) to stdout. It is the task of the receiving application to handle this. The normal terminal handles this by setting the cursor to the start of the line. Your program can intercept this character and also treat it by resetting the line.

How to write an equivalent of KeyListener with JLine?

I am working on a console application written in Java. What I have to do is handle user keyboard input. When a long process in launched with our program in a terminal, the user must have the possibility to press 'q' at anytime to stop the process (which is running on a separate thread).
I've tried several things :
Running in another different thread something that read user input and throws an InterruptedException to the process' thread
Using JLine and its ConsoleReader
Using JLine and add a TriggeredAction linked to a keyboard key
But each time, I face the same problem : the user has to press ENTER key, and I don't want that.
Thanks for help if you have any ideas, or the actual solution of my problem.
PS : Please, if you think this is impossible, don't answer. I know it's possible.
Perhaps you could use one of these libraries,
http://sourceforge.net/projects/jxgrabkey/ for linux
https://code.google.com/p/jintellitype/ for windows
Less related with your question, but I think can help you,
http://sourceforge.net/projects/javacurses/
The problem, with all of this, is that you will lost the platform independence.
To avoid having the user press enter and to be able to directly respond to a keypress, we can make use of jline3 in the following way, wherein we first change the terminal into rawmode to directly respond to keys, and then wait for the next entered character.
var terminal = TerminalBuilder.terminal()
terminal.enterRawMode()
var reader = terminal.reader()
var c = reader.read()
<dependency>
<groupId>org.jline</groupId>
<artifactId>jline</artifactId>
<version>3.12.3</version>
</dependency>

Can a Program Differentiate between User Key Input and Program key Input?

Is it possible for a program to block key input from other programs but NOT key input directly from the user's hand hitting a key on the keyboard?
For example:
ProgramA contains a text field .
ProgramB is Windows On-Screen keyboard.
After selecting the text field in ProgramA, I attempt to press the letter 'c' on the On-Screen Keyboard program but it does not appear in the text field.
However, when I press the letter 'c' directly from my keyboard it appears within the text field of ProgramA.
I know how key events work, how they are queued, and I've seen the BlockInput Function in c++
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646290%28v=vs.85%29.aspx
But that function simply blocks all input.
Ive encountered programs that filter the input in this way and can't seem to figure out how. Am I missing something completely obvious?
This question is mainly pertaining to c++ on the Windows 7 operating system.
You can never be sure if input is coming from hardware. The simplest example is a custom driver wich will simulate real keyboard driven by software input.
Some fancy idea can be attaching microphone to device, to listen for sounds/vibrations for input filtering, but do you mean complete hardware solution or application, wich will be given to personal users?

Categories