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.


>> REGISTER NOW TO START POSTING


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

Results 1 to 2 of 2

Thread: KeyListener Problem

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default KeyListener Problem

    This is my code:

    import java.awt.*;
    import java.applet.*;
    import java.awt.Graphics;
    import java.awt.event.*;


    public class playertest extends Applet implements KeyListener
    {
    int x=100;
    int y=500;


    public void paint (Graphics g)
    {
    g.setColor (Color.blue);
    g.fillRect (x, y, 15, 15);
    g.fillRect (x-6, y+3, 30, 5);
    g.setColor (Color.black);
    g.fillOval (x-2, y - 15, 20, 20);



    eyes (g);
    mouth (g);
    }


    public void eyes (Graphics g)
    {
    g.setColor (Color.white);
    g.fillOval (x + 11, y - 12, 6, 6);
    g.fillOval (x + 1, y - 12, 6, 6);

    }


    public void mouth (Graphics g)
    {
    g.setColor (Color.red);
    g.fillOval (x + 5, y - 6, 12, 5);
    }

    public void moveleft ()
    {
    x = x-2;
    }

    public void moveright ()
    {
    x=x+2;
    }


    public void keyPressed (KeyEvent e)
    {
    if (e.getKeyCode () == KeyEvent.VK_UP)
    moveleft ();
    else if (e.getKeyCode () == KeyEvent.VK_LEFT)
    moveleft ();
    else if (e.getKeyCode () == KeyEvent.VK_RIGHT)
    moveright ();
    }


    public void keyReleased (KeyEvent e)
    {
    if (e.getKeyCode () == KeyEvent.VK_UP)
    x=x;
    else if (e.getKeyCode () == KeyEvent.VK_LEFT)
    x =x;
    else if (e.getKeyCode () == KeyEvent.VK_RIGHT)
    x = x;
    }


    public void keyTyped (KeyEvent e)
    {
    }

    }

    I've been able to do this before, but for some reason nothing happens when I push the arrow keys with this code. Anyone know how to fix this?


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,296
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: KeyListener Problem

    The component that listens for KeyEvents must have focus in order to do so (see the API for info on the requestFocus method). Key bindings work better to circumvent this behavior:
    How to Use Key Bindings (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)

    On another note, please see the announcements at the top of every forum for info on how to properly format code to preserve formatting.

Similar Threads

  1. KeyListener implementation problem
    By Xepato in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 15th, 2013, 08:25 PM
  2. KeyListener
    By tim8w in forum AWT / Java Swing
    Replies: 9
    Last Post: January 25th, 2013, 12:40 AM
  3. Tetris KeyListener Problem
    By Martinnikol in forum AWT / Java Swing
    Replies: 1
    Last Post: November 5th, 2011, 07:11 PM
  4. keylistener small problem
    By matecno in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 14th, 2010, 07:51 PM
  5. Problem with KeyListener
    By r12ki in forum AWT / Java Swing
    Replies: 3
    Last Post: October 1st, 2009, 01:18 PM