hi all,
how to know user pressed a key in the keyboard expect using keyListener.
thanks a lot for your help.
Printable View
hi all,
how to know user pressed a key in the keyboard expect using keyListener.
thanks a lot for your help.
Tell us your problem first
im doing console programming in java, and i wanna know how to do the press any key to continue thing, so watever key is pressed then it does something, i know how to read user input but i dont think thats how to do it. Thanks
Use ascii code for your programming
ASCII : Java Glossary
How to obtain ASCII code of a character - Java Forums
i just wanna detect a key press
Each key press will return a key ascii code so u have to find the maximum and minimum Ascii key value form the links i given to you then make a logic like
if( minKeyAscii >= KEY_TYPED && KEY_TYPED <= maximum )
I think this will fix your problem. Try your level best, good luck.
Maybe use the Scanner class.
// Json
Something like this?
Only problem with using the Scanner class like this is you have to press Enter for it to register.Code :import java.util.Scanner; public class Cilang { /** * JavaProgrammingForums.com */ public static void main(String[] args) { System.out.println("Java Programming Forums"); System.out.println("Continue? (y/n)"); Scanner sc = new Scanner(System.in); if(sc.next().equals("y")){ DoSomething(); }else{ System.out.println("Bye!"); } } public static void DoSomething(){ System.out.println("Hello World!"); } }
I dont think its possible to just detect one keypress from the System.in inputstream unless the keypress is the return/new line key.
The problem is that the OS does not send the data to the inputstream until the return key is pressed :(
// Json
*cough* JNI.
=)) What a surprise!!
Java Native Interface - Wikipedia, the free encyclopedia
Its not my fault there is a large volume of JNI questions flooding into the forum!
haha
*cough* didnt expect that from you *cough*
Maybe we need to start putting together some nice JNI common stuff :)
// Json
Did you just use nice and JNI in the same sentence!?
Possibly :/
// Json
excuse me ., its not related on this post .. but i want to ask something.
i modified javaPF's reply(code) about this thread..
Code :public class Cilang { /** * JavaProgrammingForums.com */ Scanner sca = new Scanner(System.in); public static void main(String[] args) { System.out.println("Java Programming Forums"); System.out.println("Continue? (y/n)"); Scanner sc = new Scanner(System.in); String input = sc.next(); if (input == "y"){ DoSomething(); }else{ System.out.println("Bye!"); } } public static void DoSomething(){ System.out.println("Hello World!"); } }
i just used some boolean expression THAT IF input has a value which is 'y' then it will do the statement in the if block,
but why isnt running?
im just trying to apply some logic behind that... (i.e. if i entered 'y' then pop it up)
but i ended up in error
it only prints the ELSE statement "bye"
Because this if statement.
Code :if (input == "y") {
Should look like this to work.
Code :if ("y".equalsIgnoreCase(input)) {
There is another thread somewhere discussing why you shouldnt use the == when comparing strings.
// Json