Results 1 to 5 of 5
  1. #1
    Reskaillev is offline Member
    Join Date
    Jul 2011
    Posts
    53
    Rep Power
    0

    Default A problem with KeyListener

    Hello everyone!

    Well I did a lil' bit of Java on university but now I wanted to program an applet Pong clone.
    Problem is that I can't seem to get the keyListener listening to my input...

    So: How do I get my keylistener to work?

    Java Code:
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*; 
    
    public class main extends Applet implements Runnable,KeyListener {
    
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 2305235281454935668L;
    
    	/**
    	 * @param args
    	 */
    	int y;
    	boolean pause;
    	Graphics offscr;
    	Thread runner;
    	int Score[] = new int[2];
    	static int height,width;
    	Image offscreenImage;
    	
    	public void init()
    		{
    		pause=false;
    		this.setSize(650,400);
    		runner = new Thread(this);
    		width = getWidth();
    		height = getHeight();
    		y = height/2;
    		offscreenImage = createImage(width, height);
    		offscr = offscreenImage.getGraphics();
    		addKeyListener(this);
    		runner.start();
    		} 
    	
    	public void paint(Graphics g)
    	{
    		offscr.setColor(Color.black);
    		offscr.fillRect(0,0,width,height);
    		offscr.setColor(Color.white);
    		offscr.fillRect(20,y-50,20,60);
    		g.drawImage(offscreenImage, 0, 0, this);
    	}
    	
    	public void update(Graphics g) 
    	{
    	paint(g);
    	}
    	
    	@Override
    	public void keyPressed(KeyEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public void keyReleased(KeyEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public void keyTyped(KeyEvent e) {
    		int keyCode = e.getKeyCode();
    	    switch( keyCode ) { 
    	        case KeyEvent.VK_UP:
    	        	y++;
    				System.out.println("up");
    	            break;
    	        case KeyEvent.VK_DOWN:
    	        	y++;
    				System.out.println("up");
    	            break;
    	        case KeyEvent.VK_SPACE:
    				if (pause)
    				{ pause=false; }
    				else
    				{ pause=true; }
    	            break;
    	        case KeyEvent.VK_ESCAPE:
    	            // handle right
    	            break;
    	     }
    	}
    
    	@Override
    	public void run() {
    		while (true)
    		{
    		repaint();
    		y++;
    		try
    		{	
    			Thread.sleep (20);
    		}
    		catch (InterruptedException ex){}
    		}
    	}
    
    }
    EDIT: I noticed it might not be necessary to get a runnable aplet for a pong clone, but I still like to know why this program wasn't working

    Thanks in advance,

    Reskaillev
    Last edited by Reskaillev; 07-22-2011 at 01:21 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Does the component with the listener have the focus?

    I added a println:
    Java Code:
    	public void keyTyped(KeyEvent e) {
               System.out.println("kT e=" + e);  //<<<< Show when called
    When I click on the window then press a key it prints
    Last edited by Norm; 07-22-2011 at 01:49 AM.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    I noticed it might not be necessary to get a runnable aplet for a pong clone, but I still like to know why this program wasn't working
    You need to be more precise about "wasn't working". Is keyTyped() ever called? Add some debugging code (and remember to click in the applet window to give it focus)

    Java Code:
    @Override
    public void keyTyped(KeyEvent e) {
        [color=green]System.out.println("keyTyped: " + e);[/color]
        int keyCode = e.getKeyCode();
        // etc
    Observe the behaviour for each of the keys mentioned in that method. Read the KeyEvent API docs to see what events the keyTyped() method will find out about.

    -----

    You should observe standard Java coding conventions and give your class a descriptive name starting with an uppercase letter eg call it PongApplet rather than main.

    Consider the more modern JApplet class and Swing technologies like timers etc.

    [Edit] Slow... ;(

  4. #4
    Reskaillev is offline Member
    Join Date
    Jul 2011
    Posts
    53
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    You need to be more precise about "wasn't working". Is keyTyped() ever called? Add some debugging code (and remember to click in the applet window to give it focus)

    Java Code:
    @Override
    public void keyTyped(KeyEvent e) {
        [color=green]System.out.println("keyTyped: " + e);[/color]
        int keyCode = e.getKeyCode();
        // etc
    Observe the behaviour for each of the keys mentioned in that method. Read the KeyEvent API docs to see what events the keyTyped() method will find out about.

    -----

    You should observe standard Java coding conventions and give your class a descriptive name starting with an uppercase letter eg call it PongApplet rather than main.

    Consider the more modern JApplet class and Swing technologies like timers etc.

    [Edit] Slow... ;(
    Thanks for answering! both of you! Its much appreciated

    Well I tried the extra code (the console output) and it seems my arrowkeys (VK.UP and VK.DOWN and so on) don't respond to the keylistner...
    When typing any other key it does respond properly ...

    EDIT: to rule it out: yes my arrowkeys do work :p
    Last edited by Reskaillev; 07-22-2011 at 02:10 AM.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Did you look at the definition of keytyped vs the other key events?

    Also there are methods for getting the focus and for making a component focusable. Not all components can be in focus.

Similar Threads

  1. KeyListener Graphics Problem
    By Makesfolkslose in forum New To Java
    Replies: 2
    Last Post: 05-26-2011, 05:07 AM
  2. Problem with Keylistener, some help pls
    By syon in forum AWT / Swing
    Replies: 1
    Last Post: 01-21-2011, 02:31 AM
  3. AWT KeyListener Problem
    By plm-pusik in forum New To Java
    Replies: 15
    Last Post: 11-10-2010, 04:38 PM
  4. KeyListener problem
    By siyi90 in forum AWT / Swing
    Replies: 7
    Last Post: 02-08-2010, 11:16 AM
  5. Problem with KeyListener in Jtable
    By sandeepsai17 in forum New To Java
    Replies: 0
    Last Post: 06-30-2009, 11:14 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
  •