Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

java console key listening?

  • 07-02-2005 11:39AM
    #1
    Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭


    I want to create a loop that breaks out when the user presses Q (I have threads running in this loop).

    The loop is in the main, but I am not sure how monitor for keyboard output. Tried using the AWT keylistener but that looks like it needs to be mapped to a component. Or maybe doing something wrong.


Comments

  • Closed Accounts Posts: 256 ✭✭$lash


    Cant see why keyListener wouldnt work for this...


  • Closed Accounts Posts: 1,502 ✭✭✭MrPinK


    Off the top of my head I'd try...
    while (!done)
    {  
        char c = (char)(System.in.read());
        if (c == 'Q' || c == 'q')
        {
            done = true;
            // stop other threads
        }
    }
    
    You may need to catch IOExceptions too


  • Registered Users, Registered Users 2 Posts: 101 ✭✭enda_4


    Is it a frame your using?

    did you try adding the keylistener to the component

    "component".addkeylistener(this)

    public void keyTyped(KeyEvent e)
    {
    char keyTyped=e.getKeyChar(); // read input
    System.out.println(keyTyped); // just to check it working
    chkLetter(keyTyped); // send to a function to check the letter

    }

    // Check for letters in array
    public void chkLetter(char keyTyped)
    {
    for (int i=0; i >1 ; i++) // infinite loop just
    {
    if(keyTyped == 'Q')
    break;
    }
    }

    Code mightn't be 100%, but something similar should do it


  • Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


    It is the main() I am doing it from. MrPink, what you had I tried before (or something similar). The problem is that read() reads a line and not a character. So the program will wait for the enter key to be pressed. So you can't have a loop as such.


  • Closed Accounts Posts: 1,502 ✭✭✭MrPinK


    Yeah, that's the closest that you can get to it in java. If you want to catch a single key press then you can't use the console, it'd have to be a GUI.

    Unless of course you write some C code and have your Java prog call it from a DLL


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 3,548 ✭✭✭Draupnir


    You could always build your own Console Reading class which you could then use to read a single character and return its value. But I guess maybe thats not what you want to do.

    By the way, you probably already have it, but there is a console reader class pretty readily available which you can use to read input to the console easily. if ya need it, drop me a pm ill send you the .class file.

    if im totally missing the point then excuse me


  • Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


    Thanks guys. Actually wanted to use Java as is to create it. But no matter. Will convert it to SWT.


Advertisement