Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

java console key listening?

  • 07-02-2005 10: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