Results 1 to 2 of 2
  1. #1
    Mikelmao is offline Member
    Join Date
    Mar 2011
    Rep Power

    Default Problem with key listener

    Hey, Im new to java and new to these forums and im having a problem with my code.
    Im trying to make a simple 2d game but till now it hasnt really been working out much =\...

    I'm trying to add 3 classes as listeners 1 is mouse motion class, mouse click and keyboard class.

    The GUI class is the first one to run (after my frame class to run the JFrame, the GUI class is the JPanel) The constructor of my GUI class looks like this:

    Java Code:
    p = new Player();
    		mm = new MouseMotion(p);
    		key = new Keyboard(p);
    		refresh = new Timer(5, this);
    all the var's are declaired in the class..
    the mouseMotionListener works perfectly but the keyboard class doesnt (the mouse clicked i havnt implemented yet.). This is my keyboard class:

    Java Code:
    import java.awt.event.*;
    public class Keyboard implements KeyListener {
    	Player p;
    	public Keyboard(Player p) {
    		this.p = p;
    	public void keyTyped(KeyEvent e) {
    	public void keyPressed(KeyEvent e) {
    		int key = e.getKeyCode();
    		if(key == KeyEvent.VK_RIGHT) {
    			p.moving = 1;
    		if(key == KeyEvent.VK_LEFT) {
    			p.moving = -1;
    	public void keyReleased(KeyEvent e) {
    		int key = e.getKeyCode();
    		if(key == KeyEvent.VK_RIGHT) {
    			p.moving = 0;
    		if(key == KeyEvent.VK_LEFT) {
    			p.moving = 0;
    When i press a button the text "TEST" doesnt display in the terminal so the listener isnt doing its job...

    What am i doing wrong here and how could i fix this?

    [EDIT] I'v tried letting the class extends KeyAdapter but it also doesnt work
    Thanks for taking the time to read my problem :)

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power


    For a KeyListener to do its stuff, the component to which it is added must have the focus.

    If this is a Swing GUI, the recommended approach is to use key bindings instead.
    How to Use Key Bindings (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)


Similar Threads

  1. Problem in action listener
    By cool in forum AWT / Swing
    Replies: 4
    Last Post: 11-16-2010, 07:44 AM
  2. problem with browser Listener in linux
    By chakribobby in forum SWT / JFace
    Replies: 0
    Last Post: 06-14-2010, 03:54 PM
  3. JColorChooser modeless - Event Listener problem
    By siamino in forum New To Java
    Replies: 1
    Last Post: 04-14-2009, 12:39 AM
  4. #key listener problem
    By mij1_7 in forum New To Java
    Replies: 2
    Last Post: 02-14-2009, 09:02 PM
  5. Regarding Listener
    By adeeb in forum AWT / Swing
    Replies: 2
    Last Post: 06-10-2008, 02:00 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