Results 1 to 2 of 2
  1. #1
    oomanamemni is offline Member
    Join Date
    Feb 2008
    Posts
    1
    Rep Power
    0

    Default how to read single Keystroke without using return

    I'm wanting to be able to read in a single keystroke from the user to proceed to the next task but can't seem to find out how to without having to press enter afterwards. Is there any way to do this? Thanks

  2. #2
    jimm1 is offline Member
    Join Date
    Jan 2008
    Location
    South Africa
    Posts
    18
    Rep Power
    0

    Talking

    Is this via the console? If you want to register keystrokes, you will need to create a JFrame (Swing GUI), and then add a KeyListener, and then when the user presses a key, it will fire the "keyPressed" method, and from there you can catch the key pressed from the KeyEvent object which has been passed. You will need to read some JFrame/Swing tutorials, which explain Components, Listeners (Such as MouseListener and KeyListener) and you will also need to understand interfaces/inheritance (You implement or extend the KeyListener or KeyAdaptor).

    Like:

    JFrame frame = new JFrame();
    frame.addKeyListener(this);

    Then in the same class, you implement "KeyListener" and implement the abstract methods keyPressed(KeyEvent e), keyReleased(KeyEvent e), and keyTyped(KeyEvent e).

    Then to get the keystroke, you call one of the methods of the KeyEvent e, such as:

    char keychar = e.getKeyChar();

    or

    int keycode = e.getKeyCode();

    -------------------------------

    Hope that helps.

    For full help, read this Swing Tutorial trail:
    Trail: Creating a GUI with JFC/Swing (The Java™ Tutorials)

    It explains all about Swing, and also look for the section on "Writing Event Listeners".

Similar Threads

  1. Compiling a single class
    By ROSOBORONEXPORTCORP in forum New To Java
    Replies: 2
    Last Post: 01-28-2008, 12:27 PM
  2. convert a lot of images into a single one
    By leonard in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 04:47 PM
  3. variable to accept a single object
    By Rgfirefly24 in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 04:41 AM
  4. Replies: 1
    Last Post: 07-29-2007, 10:53 PM
  5. Replies: 1
    Last Post: 06-06-2007, 04:20 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
  •