DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Keyboard Input

  1. #1
    Join Date
    Nov 2004

    Question Keyboard Input

    We have bitten off more than we can chew.

    My team and I, for our term end Java project, are building a silly text-based maze adventure, and we've encountered some unforeseen problems.

    As the deadline approaches, i/o for the program is becoming our biggest issue. My question is:

    What is the most efficient (and preferably least bug-prone) way to display text (even if it's the output window), and receive keystroke input (that isn't JOptionPane)?

    We've considered just using the output window, but we're unsure of which listeners or event handlers to use, and if they're even able to grab this input when using the output window... (Scanner doesn't seem to work with 1.4.2). One of our group members has started delving into gui interfaces (an input field, and output pane) as well, but we're unsure of how to implement it.

    Essentially, any class needs to be able to "speak" (Have text echoed) and "listen" (wait for a keystroke).

    We'd really appreciate any help offered.

  2. #2
    Join Date
    Feb 2004
    If you're using the console window you can't listen for keystrokes as far as I know. It isn't part of your java program, and I don't think the JVM is ever notified of keystrokes from it. You can get user input from it if the user presses enter however. See the thread elsewhere in this forum named something like "Keyboard class". In it someone has posted a keyboard class that has methods for getting input from the console. You can also print text to the console window using
    System.out.print("text here");
    System.out.println("text here");
    If you want to listen to keypresses (ie the user doesn't need to press enter, just press a key) then you'll need to use a Java GUI. If you add a KeyListener to the GUI you can listen for keystrokes when the GUI window has focus. Check the API for all the methods. You can use the events it produces to learn exactly what key was pressed.

    Click here to see the API for Keylistener

    Click here to see the API for KeyEvent

  3. #3
    Join Date
    Nov 2004
    Thanks Mike!

    I was just on the phone with the group, and we think we might have come up with a solution...

    First of all, the task we'd like accomplished: A method in the Main class that is callable from anywhere in the program, that essentially halts flow, until a key is pressed. The key that is pressed is then returned to the original caller, and can then be processed as the situation dictates. (Eg: The user is prompted "pick a direction" and Main.getInput() is called. The program pauses while waiting... the user presses the N key. Main.getInput() returns 'n' to the calling class/method/whatever. )

    Or proposed solution is this:
    Start our listeners in Main. (import java.awt.event.KeyListener, and have a class that implements KeyListener)
    Have getInput call private int keyPressed( KeyEvent event ), which simply returns event.getKeyCode().
    getInput can then use a switch to take that code and turn it into a reasonable response.

    (The following code was rapidly typed during a brainstorming session, and might be missing bits...)
    public String getInput() {
        int theKey;
        while ( theKey == NULL) {
                   theKey = keyPressed();
             switch(theKey) {
             case 343: return "up"; break;
             case 443: return "down"; break;
             case 647: return "stay"; break;
    My question to you, the Gurus and Oracles of Java, is "Is this a dumb idea, a good way to do it, or the most ridiculous thing you've ever heard?

  4. #4
    Join Date
    Feb 2004
    Well you seem to have misunderstood how the KeyListener interface works. You don't ever call any of it's methods. The methods are called by the JVM when a relevant event occurs. In this case, when a key is pressed the operating system will inform the jvm, and the jvm will notify the window with focus, and this window will notify all of its key listeners.

    So if you want your program to wait until a key has been pressed you'll have to get the method in question to wait until the jvm calls the keyPressed method.

  5. #5
    Join Date
    Nov 2004
    Thanks again for the reply.

    Could you point us in the direction of a resource, or perhaps give an example?

    (Are we barking up the wrong tree by trying to build a globally accessable method that pauses the program, and then returnes the first key pressed, or should we rethink this?)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.