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

Thread: Loop ending with ESC button

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Location
    EU - Bladefist Alliance
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Loop ending with ESC button

    The whole idea is to end the loop with the ESC button.

    import java.util.Scanner;
     
    class Sum {
    	public static void main(String[] args) {
    		int sum = 0;
    		System.out.println("Please write a number, end with ESC button");
    		Scanner in = new Scanner(System.in);
    		int number = in.nextInt();
     
    		while (number != null ){
    			sum += number;
    			System.out.println("Please write a number, end with ESC button");
    		} // end while
     
    		System.out.println("The sumn is: "+ sum);
    	} // end main
     
    } // end Sum


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Loop ending with ESC button

    As far as I know there is no way to get button events with a command-line program like this.
    If I remember correctly certain operating systems only generate keyboard-events for programs that are executed in a window, so unless you create a JFrame (or use any other third party GUI library) you are out of luck.

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Location
    Finland
    Posts
    25
    My Mood
    Bored
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default Re: Loop ending with ESC button

    What Cornix said. Well, to be exact, you *could* inject a hook to for example windows dll's and get an signal from key presses with JNI/JNA. I think SetWindowsHookEx function (Windows) would be the one for windows. But yea, thats getting quite complicated.

Similar Threads

  1. Replies: 6
    Last Post: March 8th, 2014, 10:08 AM
  2. no ending after decimal point.
    By miller4103 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: April 10th, 2013, 04:29 PM
  3. Pausing a loop until a button is pressed
    By amcqueen in forum AWT / Java Swing
    Replies: 1
    Last Post: October 5th, 2011, 01:57 AM
  4. Stop a while loop with an awt button, problem
    By frx08 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 20th, 2011, 08:24 AM
  5. Weird issue with while loop ending/being skipped
    By ang3c0 in forum Loops & Control Statements
    Replies: 4
    Last Post: December 25th, 2009, 12:09 PM