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

Thread: how to know user pressed a key in the keyboard

  1. #1
    Junior Member
    Join Date
    Aug 2009
    Posts
    6
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Exclamation how to know user pressed a key in the keyboard

    hi all,

    how to know user pressed a key in the keyboard expect using keyListener.
    thanks a lot for your help.


  2. #2
    Junior Member
    Join Date
    Sep 2009
    Location
    Bangalore.India
    Posts
    9
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: how to know user pressed a key in the keyboard

    Tell us your problem first

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

    cilang (September 10th, 2009)

  4. #3
    Junior Member
    Join Date
    Aug 2009
    Posts
    6
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: how to know user pressed a key in the keyboard

    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

  5. #4
    Junior Member
    Join Date
    Sep 2009
    Location
    Bangalore.India
    Posts
    9
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: how to know user pressed a key in the keyboard


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

    cilang (September 10th, 2009)

  7. #5
    Junior Member
    Join Date
    Aug 2009
    Posts
    6
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: how to know user pressed a key in the keyboard

    i just wanna detect a key press

  8. #6
    Junior Member
    Join Date
    Sep 2009
    Location
    Bangalore.India
    Posts
    9
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: how to know user pressed a key in the keyboard

    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.

  9. The Following User Says Thank You to vivekmk For This Useful Post:

    cilang (September 10th, 2009)

  10. #7
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: how to know user pressed a key in the keyboard

    Maybe use the Scanner class.

    // Json

  11. The Following User Says Thank You to Json For This Useful Post:

    cilang (September 10th, 2009)

  12. #8
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: how to know user pressed a key in the keyboard

    Quote Originally Posted by Json View Post
    Maybe use the Scanner class.

    // Json
    Something like this?

    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!");        
        }    
    }
    Only problem with using the Scanner class like this is you have to press Enter for it to register.
    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.

  13. The Following User Says Thank You to JavaPF For This Useful Post:

    cilang (September 10th, 2009)

  14. #9
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: how to know user pressed a key in the keyboard

    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

  15. The Following User Says Thank You to Json For This Useful Post:

    cilang (September 10th, 2009)

  16. #10
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: how to know user pressed a key in the keyboard

    *cough* JNI.

  17. The Following User Says Thank You to Freaky Chris For This Useful Post:

    cilang (September 10th, 2009)

  18. #11
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: how to know user pressed a key in the keyboard

    Quote Originally Posted by Freaky Chris View Post
    *cough* JNI.
    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.

  19. #12
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: how to know user pressed a key in the keyboard

    Its not my fault there is a large volume of JNI questions flooding into the forum!

  20. #13
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: how to know user pressed a key in the keyboard

    haha

    *cough* didnt expect that from you *cough*

    Maybe we need to start putting together some nice JNI common stuff

    // Json

  21. #14
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: how to know user pressed a key in the keyboard

    Did you just use nice and JNI in the same sentence!?

  22. #15
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: how to know user pressed a key in the keyboard

    Possibly :/

    // Json

  23. #16
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: how to know user pressed a key in the keyboard

    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.

  24. #17
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: how to know user pressed a key in the keyboard

    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

Similar Threads

  1. User Input Loop
    By cfmonster in forum Loops & Control Statements
    Replies: 7
    Last Post: August 24th, 2009, 01:52 PM
  2. Libraries for User Tracking
    By jwpa in forum The Cafe
    Replies: 4
    Last Post: August 4th, 2009, 09:32 AM
  3. How to create a system wide mouse or keyboard hook?
    By Freaky Chris in forum Java Native Interface
    Replies: 17
    Last Post: June 17th, 2009, 01:06 PM
  4. How do I fix my program so I can use the keyboard to type in the numbers?
    By rocafella5007 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: April 29th, 2009, 02:39 PM
  5. How can i control keyboard through programming?
    By Mohd in forum Java Theory & Questions
    Replies: 3
    Last Post: January 5th, 2009, 07:10 AM