Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Why System.in returns -1 when I pressed "Enter key" how can I mimic "Enter key" in console?

  1. #1
    Junior Member
    Join Date
    Mar 2022
    Posts
    15
    My Mood
    Sleepy
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Why System.in returns -1 when I pressed "Enter key" how can I mimic "Enter key" in console?

    I used Robot class to mimic Enter key, and it worked as Norm suggested in https://www.javaprogrammingforums.co...t-console.html.

    However, it is dangerous to use Robot class since when focus changes while Robot class continues to work in background(i.e. the program did not end). Because, Robot continues to press keys until the end of the program it continues to press that key to each new focused windows.

    So I want to mimic "Enter Key" in console by using ANSI Escape codes such as by writing "\13"(CR) and "\10"(LF) to console. But not success.

    I want to use below code to which character sent does "put entered input to stream" functionality as Enter key does. I used below code :

    import java.io.*;
     
    public class Main
    {
        public Main() throws IOException {
            doInput();    
        }
     
        private void doInput() throws IOException{
            InputStream in=System.in;
            int read=0;
            read=in.read();
            w(" readed integer is :"+Character.getNumericValue((char)read)+"..");
        }
     
    	public static void main(String[] args) throws IOException{
    		new Main();
    	}
     
    	private static void w(String s) {
    	    System.out.println(s);
    	}
    }

    When I just "Enter key" to send as input, output becomes :

    readed integer is :-1..
    I tried to send -1 integer to console using System.out( after reading these two PrintStream - System.out Java doc and PrintStream's print(int i) method in Java doc) it just wirtes -1 to screen, it does not mimic "Enter key pressed". Also I send "\13" and "\10"(i.e CR and LF ANSI escape codes ) but it does not mimic anything other than LF (just goes to next line). Output is simply "a-1".

    1. Which ANSI escape code mimics "Enter Key pressed" in Unix/Windows terminal console?
    2. Why "\10" or "\13" (i.e. LF and CR ASCII chars?) does not mimic end of input?
    3. How can I mimic "Enter key", which character I have to send to console to put input before it to InputStream(i.e. System.in) ?

    Any help is appreciated.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Why System.in returns -1 when I pressed "Enter key" how can I mimic "Enter key" in console?

    When I just "Enter key" to send as input, output becomes :

    readed integer is :-1..
    Read the API doc for the getNumericValue method to see when it returns a -1.
    A better way to see what is in the read variable: Integer.toHexString(read)
    or String.valueOf(read)
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2022
    Posts
    15
    My Mood
    Sleepy
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Why System.in returns -1 when I pressed "Enter key" how can I mimic "Enter key" in console?

    Quote Originally Posted by Norm View Post
    Read the API doc for the getNumericValue method to see when it returns a -1.
    A better way to see what is in the read variable: Integer.toHexString(read)
    or String.valueOf(read)
    Thanks Norm, as you suggesterd I read the API doc and I found that "If the character does not have a numeric value, then -1 is returned." Then your suggestion String.valueOf() gives thecorrect number(s) for "enter" which is integer 13 and 10 in order.

    --- Update ---

    Can I pipe System.out to System.in using pipe? Does anybody knows anything about it?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Why System.in returns -1 when I pressed "Enter key" how can I mimic "Enter key" in console?

    Did you look at the PipedInputStream and PipedOutputStream classes?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2022
    Posts
    15
    My Mood
    Sleepy
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Why System.in returns -1 when I pressed "Enter key" how can I mimic "Enter key" in console?

    I looked, actually I have managed to direct System.out to System.in. However, it did not solve my problem since I found that my problem is due to that the console stucks in System.in.read() method and wait until enter key which I could not find a way to mimic it. It is a vicious cycle.Robot class is safe in Java programs having GUI since focus events can be managed. But in console Java programs extra caution needed to "consume its events" before sending to console's buffer.

    I read that ANSI escape codes does lots of tricks and even terminal programs are purely uses ANSI escape kodes to perform tasks. But console on that terminal is Java's output screen. So I can not find a feasible solution to mimic "Enter Key" like "Robot Class" using pure ANSI escape codes.

  6. #6
    Member
    Join Date
    Apr 2022
    Posts
    36
    Thanks
    0
    Thanked 8 Times in 8 Posts

    Default Re: Why System.in returns -1 when I pressed "Enter key" how can I mimic "Enter key" in console?

    InputStream in = System.in;
    int read = in.read();
    System.out.println(".");
    System.out.println((char)read);

Similar Threads

  1. Replies: 1
    Last Post: December 16th, 2021, 08:12 AM
  2. Replies: 2
    Last Post: May 22nd, 2014, 01:17 PM
  3. Replies: 3
    Last Post: December 2nd, 2012, 11:18 PM
  4. password field, with focused "Enter" button
    By chronoz13 in forum Java Swing Tutorials
    Replies: 1
    Last Post: June 13th, 2010, 05:00 PM
  5. password field, with focused "Enter" button
    By chronoz13 in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: June 13th, 2010, 05:00 PM