Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: KeyListeners and KeyEvents

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Thanked 3 Times in 2 Posts

    Default KeyListeners and KeyEvents

    Hey, I decided that because when I set out to make a game 5 days ago, and I couldn't because I didn't know how to control stuff with the keyboard, I needed to learn how to create a program that could do something like this, but I had no idea how. I searched on the internet for hours, and eventually gave up and resorted to mouse events, from copied and pasted code. Then, someone contacted me and asked me to send them the code I had used for KeyListeners, and I told them I hadn't because I didn't know how, and that kind person gave me this gem.

    The first thing you need to do is in the class you want to move, you must implement KeyListener
    public class Example implements KeyListener{
    /* Other game stuff (updates, constructors, other stuff) */
    In the class that you implemented KeyListener, you need the instance of the window or canvas that you're using and you need to do
    (instance of game window or canvas).addKeyListener(this);

    Then you need to add 3 methods in the same class because they are abstract in KeyListener and need to be over written since you're implementing KeyListener
    public void keyPressed(KeyEvent e){
    }//key being held down
    public void keyReleased(KeyEvent e){
    }//key released
    public void keyTyped(KeyEvent e){
    }//not exactly sure, I never use
    After that, all you need to do is make the game recognize the keys and give it actions for them
    In public void keyPressed(KeyEvent e) or whichever void you want to use that you just added (the code above), you need to add
    public void keyPressed(KeyEvent e){
    int key = e.getKeyCode();
    Then you have to check the int against the possible codes for keys (java has them built in)
    public void keyPressed(KeyEvent e){
    int key = e.getKeyCode():
    if(key == KeyEvent.VK_SPACE){
    System.out.println("Pressed Space");
    would say Pressed Space whenever you hit space
    if your using eclipse, you can get all the keycodes when you type if(key == KeyEvent.), it'll bring up the keycodes

    After that I was fine, and I was able to make my game functional!

    Hooray for that!

  2. The Following User Says Thank You to worsewicked For This Useful Post:

    JavaPF (January 9th, 2012)

  3. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Thanked 833 Times in 772 Posts
    Blog Entries

    Default Re: KeyListeners and KeyEvents

    Glad you got your game functioning. To add to your post, here are two tutorials which are helpful on this subject:
    How to Write a Key Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)
    How to Use Key Bindings (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)
    One should also note that for a KeyListener to function, the component must have focus (KeyBindings can be configured to overcome this). One should also read the API for KeyEvent - noting the difference between the keyTyped/keyPressed/keyReleased may produce different values for the getKeyCode/getKeyChar methods.

Similar Threads

  1. [SOLVED] KeyListeners Require Focus...
    By snowguy13 in forum AWT / Java Swing
    Replies: 2
    Last Post: January 6th, 2012, 08:15 AM
  2. Question about KeyListeners
    By Jams in forum Java Theory & Questions
    Replies: 5
    Last Post: November 6th, 2011, 10:08 AM
  3. KeyListeners: Automatic Focus?
    By bgroenks96 in forum Java Theory & Questions
    Replies: 32
    Last Post: June 24th, 2011, 09:03 PM
  4. Help - Swing Timer, 2 KeyEvents
    By Gheta in forum AWT / Java Swing
    Replies: 2
    Last Post: July 29th, 2009, 02:46 PM