Results 1 to 5 of 5

Thread: Keylistener

  1. #1
    dyelax is offline Member
    Join Date
    Oct 2010
    Posts
    41
    Rep Power
    0

    Default Keylistener

    Hey guys I'm trying to program a game of snake but my keylistener to change directions isn't working. I've set it up like all the examples I've seen online and I can't see what I'm missing. I've made a SSCCE for just the up direction (either 'w' or the up arrow key):
    Java Code:
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class GameSSCCE extends Applet implements KeyListener{
    	private String direction;
    
    	public void init(){
    		setSize(300,100);
    		direction = "No keys have been pressed.";
    		addKeyListener(this);
    	}
    	
    	public void paint(Graphics g){ g.drawString(direction, 60, 60); }
    	
    	public void keyPressed(KeyEvent e) {	
    		if(e.getKeyChar() == KeyEvent.VK_W || e.getKeyChar() == KeyEvent.VK_UP){ 
    			direction = "Up.";
    		}
    		repaint();
    	}
    	public void keyReleased(KeyEvent e) {}
    	public void keyTyped(KeyEvent e) {}
    }

  2. #2
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Keylistener

    KeyEvent.VK_W is a key code, not a key char.
    Use the getKeyCode() method instead of getKeyChar()

  3. #3
    dyelax is offline Member
    Join Date
    Oct 2010
    Posts
    41
    Rep Power
    0

    Default Re: Keylistener

    Thanks! That was part of the problem. another part was that the applet did not have the keyboard focus originally so I had to click on it before typing. I put in setFocusable(true); and that fixed it.
    Last edited by dyelax; 09-20-2012 at 06:59 PM.

  4. #4
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Keylistener

    I believe you can use the requestFocus(); method. Add it to the paint method.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Keylistener

    But note that the recommended method to use is requestFocusInWindow().

    Does your class extend Applet or JApplet?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Need help with KeyListener
    By McDucky in forum New To Java
    Replies: 3
    Last Post: 09-16-2012, 05:35 PM
  2. Help with KeyListener
    By armyson in forum New To Java
    Replies: 1
    Last Post: 11-25-2011, 12:56 PM
  3. Help with keylistener?
    By Kaizo in forum New To Java
    Replies: 4
    Last Post: 12-11-2010, 12:55 AM
  4. keyListener not doing anything
    By imorio in forum AWT / Swing
    Replies: 10
    Last Post: 08-17-2010, 10:46 PM
  5. KeyListener - Is this what I need?
    By dbashby in forum New To Java
    Replies: 26
    Last Post: 04-18-2009, 04:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •