Input in Java without pressing enter - java

I want to take any input in Java without pressing the enter key. Actually I want to make a password input program, so that as soon as the user enter any character, I can clear the screen and replace the characters with an equal number of asterisks ("*"), like the typical password entries made anywhere in the web. Most importantly, I would like to do the same in the Java Terminal Window, not by using those applet methods. Please tell me if i would have to supply more details. I am using BlueJ.
Thank you,
Mayank.

You may want to have a look at the Console's readPassword(...) methods (needs Java 6+).
Or, when using Java 5 (or less), have a look at this article: Password Masking in the Java Programming Language, especially the paragraph Command-Line Input Masking.

Related

is it possible to write words on other pages like google search box from java programs

I saw a macro program that writes words on anywhere when user click a key on keyboard.(write words that user typed in advance)
But when I study programing i learned just printing out on console or my project like use "system.out"or "setText/print"
problem
I just tried to get curser focus and write words but i can't go forward.
Is it possible to write something from java project to another programs??
I'm not sure whether I understood your question correctly, but you can 'inject' user inputs with the AWT Robot API:
https://docs.oracle.com/javase/7/docs/api/java/awt/Robot.html
Here's an example you can take a look at.

How to clear the console screen when introducing input in Java?

Is there a way to clear the console when the Java program detects the user it's introducing some data?
I'm executing a program that does the following:
User: //enter username
//show data of the username
I want to make it so the user can search for as many users he want until he ends the execution.
To make it clear and nice, after each username search, I want to clear the screen when the program detects the user is typing something (filling the buffer).
Is there any way to do this?
This is the code I'm using to clear the console (this does not detect when the user is typing):
public static void clearScreen() {
System.out.print("\033[H\033[2J");
System.out.flush();
}
Thanks in advance.
You cannot use System.in without pressing the ENTER key at the end. Its the only way. System.in is not aware of the user input until enter is pressed. So if you are using a normal command line to do this, this won't work and you have to stick different raw command lines like jline
For example :
Terminal terminal = TerminalBuilder.builder().system(true).jna(true).build();
terminal.enterRawMode(); //this enters into a raw mode and get's input on reader
reader = terminal.reader();
//finally
reader.close();
You can check it out. Although it highly doesn't make sense to bring in more dependencies unless you really want to and could just stick to a simple while loop
If you are using maven here's the dependency you can use.
<dependency>
<groupId>org.jline</groupId>
<artifactId>jline</artifactId>
</dependency>
By default, a terminal can only output text line by line.
Moving the cursor around, changing colors or clearing the screen requires special escape sequences that are interpreted by the terminal emulator in which you run your application.
C programs in Linux would usually do this via the ncurses library.
Java programs can do something similar with Lanterna (https://github.com/mabe02/lanterna) but within a GUI window. See also this article, which names other alternatives and shows how to use Lanterna: http://rememberjava.com/cli/2017/01/22/ncurses_terminal_libs.html

How do you set a pre-determined text as a user input with buffered reader?

Basically, how would I ask for a user input which would give out a default value right from the begenning for the user to edit. Eg. Asks the user to give a value while a default "1" is already typed in the text-field.
Calling the readLine() method on a BufferedReader doesn't create a "text field" analogous to a text input in a GUI application.
In theory what you want to do could be possible using a library such as RawConsoleInput. You would have to write a method which keeps track of the "input" in a stack of characters which is initialised to contain the default value. Then print the default value, and read keypresses one-by-one:
For most keypresses, push the key pressed to the stack, and print it so it appears on the console.
When the backspace key is pressed, pop from the stack and print the \b escape sequence to simulate a backspace in the console.
On the enter key, stop reading keypresses, print \n and return the stack contents as a string.
This solution will not be cross-platform compatible, because there is no cross-platform way to do non-line-buffered console input, and some consoles (e.g. the one built into Eclipse) don't handle \b properly.
The much simpler and almost certainly better way to have a default option is just to tell the user what the default is, and let them press 'enter' without typing anything to get the default.
If you're using a console, that's not going to work. It only reads what actually comes in through the input stream. You can display a "1", but that is output, not input. Also, don't expect them to be able to delete the "1".
What you can do is assume a default value if they don't enter anything.
You can also collect their input and just stick a 1 at the beginning of the string after you read it.
Also, why are you trying to do this?

Is there a way to print text after the line where the user inputs?

I'm looking to print text on a line that is AFTER where the user is inputing their text.
String userInput;
System.out.println("Hello");
userInput = In.getString();
//I want a System.out.print(""); right here, but I want it to appear while
//the user is still typing their input.
Print: "Hello"
User is getting input
Print "I am printing this at the same time that the user is typing"
As mentioned in the comments, keystrokes cannot be captured in raw java console applications.
His could be do e using gui applications like using AWT/Swing GUI.
Since this could be an overkill for your problem, you should know that java suports editing of console outputs in a way.
E.g. if the current output has hello, printing \b character will erase one character from console and it will look like hell.
Perhaps you can think in these lines and build your functionality.
You can put console into raw mode. There is no built-in platform-independent way of getting there. This might be a solution you can implement,
Non blocking console input in Python and Java
got it from here. There are more solutions in there that might be helpful

Filter character of console input in Java

I'm writing a terminal version of my Java application, and this is the first time I do this. I tried Scanner and Console, but as far as I investigated, these classes only allow me to receive user input after he/she finish inputting (no manipulating/filtering).
For example, I want the user inputs his age, but if he inputs a meaningless string, all I can do is validating the string and requesting him to input again. What I really want is only allow him to input integer, i.e when he press any key which is not a number, the character will not be displayed on console. Using Swing and JTextField I can do this easily with DocumentFilter, but with console only, I still haven't found a way.
Any help is appreciated. Thanks all.
Java has something called robot API which u can use to control keyboard mouse etc...
Not the recommended way :)
Since the console is not Java program (its a c program) you have limited way to interface with it, perhaps you can write native API to get more control over it. I have not tried it but if needed use this option.

Categories

Resources