Press Anykey in Java using BufferedReader

How to detect keyboard input when the user presses any button, and then doSomething / Repeat Method if the exit button is without swing / awt?

public static void isChecking(String x)throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String anykey = null; System.out.print("Press Anykey to Continue : "); anykey = br.readLine(); //if pressanykey main(null); //call main class //if escape button System.out.println("Good Bye "); System.exit(1); } 

thanks
Mrizq

+4
source share
4 answers

Unable to detect KeyPress in java with console, I think. Although there is a way to do this initially using JNI. You can get the source code example here

As for continuous input until break, you can do this with a simple while loop:

 while((input = in.readLine()) != null){ System.out.println(); System.out.print("What you typed in: " + input); } 
+1
source

How about a simple loop:

 boolean escapeIsNotPressed = true; while (escapeIsNotPressed) { anyKey = br.readLine(); if (anyKey.equals(espaceCharacter)) { escapeIsNotPressed = false; } else { main(null) } } 

Not sure what a string representation of an escape character is. Try to show it using System.out.println (any key) and entering it into your code.

0
source

Note. The escape button is not a character to be passed through System.in. In addition, you use the readLine method, so if the user enters "abc" and then enters, your anyKey variable will contain "abc".

Basically you need to listen for events on the keyboard. Check out this tutorial http://download.oracle.com/javase/tutorial/uiswing/events/keylistener.html .

0
source

try this way

 public void keyPressed(KeyEvent e) { while(!e.keyCode == Keyboard.ESCAPE) { //do something } } 
0
source

All Articles