hi all,
how to know user pressed a key in the keyboard expect using keyListener.
thanks a lot for your help.
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.
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
cilang (September 10th, 2009)
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
cilang (September 10th, 2009)
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.
cilang (September 10th, 2009)
Maybe use the Scanner class.
// Json
cilang (September 10th, 2009)
Something like this?
Only problem with using the Scanner class like this is you have to press Enter for it to register.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!"); } }
Please use [highlight=Java] code [/highlight] tags when posting your code.
Forum Tip: Add to peoples reputation by clicking the button on their useful posts.
cilang (September 10th, 2009)
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
cilang (September 10th, 2009)
*cough* JNI.
cilang (September 10th, 2009)
What a surprise!!
Java Native Interface - Wikipedia, the free encyclopedia
Please use [highlight=Java] code [/highlight] tags when posting your code.
Forum Tip: Add to peoples reputation by clicking the button on their useful posts.
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..
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"
Last edited by chronoz13; September 11th, 2009 at 04:42 AM.
Because this if statement.
if (input == "y") {
Should look like this to work.
if ("y".equalsIgnoreCase(input)) {
There is another thread somewhere discussing why you shouldnt use the == when comparing strings.
// Json