Results 1 to 12 of 12
  1. #1
    Sreejesh25 is offline Member
    Join Date
    Apr 2008
    Location
    Banglore,India
    Posts
    4
    Rep Power
    0

    Question getting each character from keyboard

    I want to read each character from the keyborad to do some action on each key.
    But the System.in.read() was waiting for ENTER key to be pressed.
    I want System.in.read() returning one character each time I'm pressing one character on the keyboard.
    Please help...
    The code is like this.

    int ch;
    while ('\n' != (ch = System.in.read())) {
    System.out.println("You pressed " + ch
    + ".System waiting for next character.Press ENTER to exit");
    }



    System.in.read() should not wait for the user to press the ENTER key.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    This may help you. I do something different here.

    Java Code:
        char getCha;
        int tmp = 0;
        InputStreamReader inReader = new InputStreamReader (System.in);
        OutputStreamWriter outWriter = new OutputStreamWriter (System.out);
    
            try{
                while(tmp != -1){
                    tmp  = inReader.read ();
                    getCha = (char) tmp;
                    outWriter.write(getCha);
                }
            }
            catch(IOException e){
                System.out.println (e.getMessage());
            }

  3. #3
    Sreejesh25 is offline Member
    Join Date
    Apr 2008
    Location
    Banglore,India
    Posts
    4
    Rep Power
    0

    Angry Still problem exists

    hi Eranga,
    I added one System.out.println() in your code.Still the problem exists.
    What I want is , whenever user pressed a key in the keyboard, some action should be triggered(Here i gave a print). But now only when user is pressing ENTER key all action are triggered sequentially. i don't want to use ENTER key.
    Please check.....

    Java Code:
        char getCha;
        int tmp = 0;
        InputStreamReader inReader = new InputStreamReader (System.in);
        OutputStreamWriter outWriter = new OutputStreamWriter (System.out);
    
            try{
                while(tmp != -1){
                    tmp  = inReader.read ();
                    System.out.println("You pressed one key");
                    getCha = (char) tmp;
                    outWriter.write(getCha);
                }
            }
            catch(IOException e){
                System.out.println (e.getMessage());
            }
    Last edited by Eranga; 04-02-2008 at 06:53 AM. Reason: CODE tags are included

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Ok, as much as possible put your output here. Hurry up. I'll explain it to you, rather putting the code directly.

  5. #5
    Sreejesh25 is offline Member
    Join Date
    Apr 2008
    Location
    Banglore,India
    Posts
    4
    Rep Power
    0

    Default Output

    This is the output I'm getting.
    Here I typed "abcd" as input

    ********************
    abcd
    You pressed one key
    You pressed one key
    You pressed one key
    You pressed one key
    You pressed one key
    You pressed one key
    *********************

    The output I'm expecting is ,
    ******************
    a
    You pressed one key
    b
    You pressed one key
    c
    You pressed one key
    *******************

    I don't want to wait for the ENTER key to trigger an action.
    please help

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Grate, see there you have press four keys in a row. And I think after that you have press the Enter key also, right? But the output you have entered here is wrong. Did you copied it from the console or just type it here?

    You should get the five lines there. Not six as you listed here. Only one line additionally should be there. If you type five characters you get six lines, for 10 characters you get 11 lines, and so on.

    So what is the addition line. It's the newline. Enter key of the keyboard also have a character, that is the newline. So in your key sequence, 'abcd' you have new line at the end.

    So you have to avoid that newline character. Now change the while loop as follows.

    Java Code:
            tmp = inReader.read ();
            getCha = (char) tmp;
    
            if(getCha != '\n'){
                System.out.println("You pressed one key " + getCha);
            }
    See what happened.

    I think on your first post, you try to exit the process by pressing Enter key. Normally it is not proper way to do. I think you already see it, in may application ask to press the key Q or any other key for exit. Not the Enter key.

    If you still want to use the Enter key for it, I think it is not easy. It can but not easy.

    Hope this is help to you.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Sreejesh25 View Post
    I don't want to wait for the ENTER key to trigger an action.
    Hmm, I never try such thing. May be action listeners may help, but I'm confusing that how to avoid Enter key from there. ;)

  8. #8
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    8

    Default

    This has been asked here before not long ago. As far as i'm aware, you cannot use System.in without pressing the ENTER key at the end. Its the only way..
    Did this post help you? Please me! :cool:

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  10. #10
    Join Date
    Jan 2011
    Location
    Jamaica
    Posts
    1
    Rep Power
    0

    Default 4 Mistakes While Starting The Internet Matter

    4 Mistakes While Starting The Internet Concern

    There are some potential mistakes that you do when your new internet function started. At the beginning of the activity, the mistakes can frame your online concern agreement with obstructed.
    What is it? Sort out 4 Mistakes while starting the internet business.
    The fundamental: throw away all the funds exchange for the ads in harmonious place.
    This can betide on the newbie. It is possible that because the spirit of the internet company started, and all funds as the ads is merely dead beat to advertise in equal place. With the surmise that the bulk of the ad, the ad can get devoted success. This is least dangerous.
    Emulsion: dejected funds in behalf of the ads you into some parts.
    Testing and measuring the results. If well-to-do, carry on with to struggle to wax monstrous ad.
    But if not, you can struggle to emigrate to other ad providers. Wise you can measure and compare the unchanging of effectiveness of other ads. If there is a new ad ritual, do not be red-faced to try.
    super investments program

    The Defective: Does not after to bribe products of learning in the internet.
    does not after to acquire products of scholarship in the internet, either the ebook or video training or membership training.

    Liquid: Buy it.
    Why should buy? Simple, vindication is to condense your deliberate over time. That's it. As you all cognizant of that assorted internet marketing products is a synopsis of the individual forms of insight from teachers online business. You can sort out many products of lore in programselection.com/online-business-collection.html

    the third: confer up after trying on the contrary once
    Profuse people broadcast up quickly because the results possess not appeared. Imagine a mod post the article decidedly, then immediately call for a blog along money. bolstering once, then not in any way do it again. Hopefully this on no occasion happens to you.

    Elucidation: venture again, again, again and again.
    Not at all offer up! endure to collection consistent.


    the fourth: does not follow up the prospects.
    Prospect is a implied user's product or service. No keep abreast of up or sort a offer as jet as doing something, but half way. Peradventure they lose or are busy, so you need to cue them via the consolidate up.

    Solution: do obey up. And not unprejudiced once.
    When you trouble up to 10 times. If not in the money, stipulate the offshoot or submit other services that may be needed by way of the prospect. Accordingly, some products contain become their own advantage. When the view has not be paired with unified upshot, may be paired with other products.


    How do you think?
    have planned a slate of mistakes that beginners usually do in the birth of the internet business?

    __________________

    money investment

  11. #11
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    441
    Rep Power
    6

    Default

    What if we use Console ?
    Mak
    (Living @ Virtual World)

  12. #12
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Quote Originally Posted by makpandian View Post
    What if we use Console ?
    Well, this seems like a resurrection... Probably some spammer brought this dead thread back to life...

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

Similar Threads

  1. getting each character from keyboard
    By Sreejesh25 in forum Advanced Java
    Replies: 6
    Last Post: 03-05-2010, 07:12 PM
  2. Polled keyboard input through swing
    By Prometheus in forum Advanced Java
    Replies: 2
    Last Post: 02-04-2008, 04:05 PM
  3. Assign a keyboard key to a JButton.
    By gszauer in forum AWT / Swing
    Replies: 1
    Last Post: 12-15-2007, 10:42 PM
  4. reading text character by character
    By bugger in forum New To Java
    Replies: 2
    Last Post: 11-09-2007, 08:54 PM
  5. Help with keyboard events?
    By Bibendum in forum New To Java
    Replies: 2
    Last Post: 11-02-2007, 02:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •