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 11 of 11

Thread: How Java handles and simulates ANSI keycode input in console?

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

    Default How Java handles and simulates ANSI keycode input in console?

    My questions are :
    1. Can pure Java console application handle ANSI keycodes such as Enter, Function keys etc. when they entered as input by user?
    2. How can I simulate that "user pressed enter key" and capture it in console?

    My aim is handling ANSI key codes for special keys such as Enter,SpaceBar,Control, Alt etc. in console application and simulate them to make simple AI which programatically enter/accepts/select next or previous menu options (i.e. like a robot) that are shown in the console. For testing purposes of console program's all menus, for instance shift is for menu-up, enter is enter to menu and accept, alt-right is for next menu etc. Can Java do this?

    Thanks.
    Last edited by grogger; April 8th, 2022 at 09:28 AM. Reason: correct words in title

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

    Default Re: How Java handle and emulate Ansi keycode input in console?

    Have you looked at the Robot class?
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    grogger (April 7th, 2022)

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

    Default Re: How Java handle and emulate Ansi keycode input in console?

    Oh my god! There have been already a Robot class in Java ! Thanks Norm !

    --- Update ---

    I wrote below only reading Robot Class's java documents, without trying Robot class in console, so I saw that below ones are misguiding at the end. So, please look at the next posts below to correct statements with sample code.

    Sadly Robot class requires awt class that means GUI components since it simulates keyboard and mouse input but it requires "an applicaton to run on it" not work(I corrected my grammer mistake) in console program.

    I also must noted that I did not checked that whether Robot class's mouse click onto console/terminal window in Linux and then entering keyboard inputs. Maybe it could do this, but it is not what I looked for, it requires extra extra coding and looks not a very infeasible solution to me. That is sad since I was very happy when I first saw Robot class! However, maybe java's third party classes like jcurses/ works...
    Last edited by grogger; April 8th, 2022 at 07:26 AM.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How Java handle and emulate Ansi keycode input in console?

    not workes in console program.
    Did you try writing a console program that used the Robot class?
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    grogger (April 8th, 2022)

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

    Default Re: How Java handle and emulate Ansi keycode input in console?

    Sorry for wrong grammer in "...not workes in console program...." I corrected it in my post too.

    Norm, actually I did not since I assumed that "it requires lot of extra code to get rid of awt related things". But not ! Thanks again. I managed to do what I want without using any awt elements. My below code with "Robot class" in console worked :

     
    import java.awt.*;
    import java.awt.event.*;
     
    public class KeyboardRobot{
     
        public KeyboardRobot() {    } // constructor method
     
        public static void main(String[] as) throws AWTException {
     
            Robot consoleRobot=new Robot() ;
            int AkeyCode= KeyEvent.VK_A; 
            int EnterkeyCode= KeyEvent.VK_ENTER; 
     
            consoleRobot.keyPress(AkeyCode);
            consoleRobot.keyRelease(AkeyCode);
     
            consoleRobot.keyPress(EnterkeyCode);
            consoleRobot.keyRelease(EnterkeyCode);
     
            System.out.flush(); // I found that writing everything in the out stream to output is necessary for Robot, otherwise it does not write anything to screen
     
        }
     
         private static void w(String s) {
            System.out.println(s);
        }
    }

    However, my code has an unexpected result : it writes the Robot's keyboard inputs to the console after end of the program too! It looked much like an "echo" command in Linux. It seems repeat all keyboard inputs of Robot class to linux terminal. Is that due to terminal's interpretation or JVM? I suspected that it is due to linux terminal since it occurs when JVM ended code successfully; somehow terminal "stores all the input of Robot's keyboard events" and flush them to console's standart output automatically after JVM. How can I prevent that "annoying side effect"? Any help is appreciated.
    Last edited by grogger; April 8th, 2022 at 07:26 AM.

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How Java handle and emulate Ansi keycode input in console?

    Can you describe how you ran your test?
    How many windows were open?
    Where was the focus when you ran the program?
    Where did you enter the java command to execute the program?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: How Java handle and emulate Ansi keycode input in console?

    Quote Originally Posted by Norm View Post
    Can you describe how you ran your test?
    How many windows were open?
    Where was the focus when you ran the program?
    Where did you enter the java command to execute the program?
    Of course;
    1. I opened a one LXTerminal.
    2. I entered all the necessary commands to compile and run my above Java program inside that terminal.
    3. I did not press any key during program's execution.
    4. Therefore while I was running my code, I was inside terminal window that had the only focus.
    Last edited by grogger; April 8th, 2022 at 07:58 AM.

  10. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How Java handle and emulate Ansi keycode input in console?

    Sorry, I do not have Linux and do not know what the OS does when a program is executed in a command prompt window on that system.
    What do you see in the window when the program is executed?

    Here is what I see in a Win10 command prompt window (renamed pgm to TestRobot):

    D:\JavaDevelopment\Testing\ForumQuestions13>java TestRobot

    D:\JavaDevelopment\Testing\ForumQuestions13>a
    'a' is not recognized as an internal or external command,
    operable program or batch file.

    D:\JavaDevelopment\Testing\ForumQuestions13>
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: How Java handle and emulate Ansi keycode input in console?

    Quote Originally Posted by Norm View Post
    Sorry, I do not have Linux and do not know what the OS does when a program is executed in a command prompt window on that system.
    What do you see in the window when the program is executed?

    Here is what I see in a Win10 command prompt window (renamed pgm to TestRobot):
    Ok, I am happy to see that, it is not related with Linux terminal. I saw similar statement as you saw in Windows command prompt:

    $ $ a
    bash: a: no such command
    $
    The second "$" sign shows that the next prompt line is in waiting state. So,Robot succesfully pressed an "Enter key" both in program and in terminal.

    --- Update ---

    Actually, I am on the clue why this happened. The Robot class wrote to standart output (std.out) as Java print() function does. So, inside a terminal, console buffer stores all the entered keyboard inputs and after program ended by JVM, console buffer releases all of them if they are in standard out's stream.

    I found a way to redirect std.out to a file in https://www.oreilly.com/library/view...1/ch01s06.html. As in https://www.rgagnon.com/javadetails/java-0453.html, I changed standart output to "/dev/null" for omiting them, but this time, nothing seen in the screen.

    I am now septical about Robot class's use is proper for my purpose. I think it eventually guide me to the same functionality of FileInputStream at the end... Since while I redirected the output I must use file instead of standart output. Then all codes transformed to simply reading and processing strings from file... with I/O stream functions of Java.

  12. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How Java handles and simulates ANSI keycode input in console?

    Robot class wrote to standart output (std.out)
    I'm not sure what that means.

    Here is a test:
    Add this statement first thing in the main method:
            try{Thread.sleep(5000); }catch(Exception x) {}  // wait so focus can be set
    Change directory to the folder with the java file and compile it. Enter the command to execute the class but do NOT press Enter.
    Open a second command prompt window.
    Leave that window and go back to the first window and press Enter to start execution of the program
    Quickly leave that window and go to the second window and give it the focus.
    The program running in the first window will type an "a" and press Enter in the second window.

    Or instead of a second command prompt window, open a text editor and give it the focus after starting the program in the first window. An 'a' should be typed in the editor.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: How Java handles and simulates ANSI keycode input in console?

    Quote Originally Posted by Norm View Post
    Quote Originally Posted by grogger
    Robot class wrote to standart output (std.out)
    I'm not sure what that means.
    Sorry, it is my mistake, it should be "...Robot class wrote to standart output (System.out)...", not "std.out". I will check your two terminal(command prompt) method.

Similar Threads

  1. Can the same scanner input handle more than one datatype?
    By SamJava_the_Hut in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 19th, 2019, 06:55 AM
  2. Console skips first input in a function and goes forward to next input.
    By chipsslave in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 18th, 2019, 06:59 AM
  3. I need help on creating a method to handle minimum and maximum input values
    By Manzanita in forum Object Oriented Programming
    Replies: 1
    Last Post: April 2nd, 2014, 02:22 AM
  4. Problem with Console Input from Clipboard
    By Nilhanth in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: March 1st, 2010, 06:47 PM
  5. [SOLVED] help with console input strnig....
    By mdstrauss in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: August 17th, 2009, 03:59 AM

Tags for this Thread