AddKeylistener() Kevin Glass' code problem

Hi !
I believe that you hate me in this community ;D so whats the problem ?? Pratically I don’t know why the keylistener doesn’t work …

public GameView(){
    //System.out.println("hey");
    JFrame container= new JFrame("Loderunner");
    JPanel panel= (JPanel) container.getContentPane();
    panel.setPreferredSize(new Dimension(460,460));
    panel.setMinimumSize(new Dimension(460,460));
    panel.setLayout(null);
    setBounds(0,0,460,460);
    panel.add(this);
    setIgnoreRepaint(true);
    requestFocus();
    addKeyListener(new KeyHandle());
    container.pack();
    container.setVisible(true);
    container.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    createBufferStrategy(2);
    strategy=getBufferStrategy();
    loadLevel("level.txt");
}

This class is in GameView class

private class KeyHandle extends KeyAdapter {
    
    public void KeyPressed(KeyEvent e){
    System.out.println("hey");
    if(e.getKeyCode()==KeyEvent.VK_LEFT){
        System.out.println("hey");
        runner.setRunning(true);
        runner.setDX(1);
    }
        
    }
    
    public void KeyReleased(KeyEvent e){
    
    if(e.getKeyCode()==KeyEvent.VK_LEFT){
        runner.setRunning(false);
        runner.setDX(0);
    }
    }
        
    }
    
}

I put some System.out.println but if i press left they print nothing , so i believe that the problem is in the costructor …

Looks like you’re adding the key listener to the wrong object - is GameView a JFrame? If so, why are you creating a new one called “container”. If you’re using the “container” JFrame you might try:


container.addKeyListener(new KeyHandle());

The other option I’d try is:


panel.addKeyListener(new KeyHandle());

Not sure which component owns focus by default, it’s been a while since I touched swing.

Cheers,

Kev

GameView extends Canvas like in the tutorial … so i try with container and panel but nothing… No one print of System.out.println() … :o

Could you post the whole code, cause it looks like you’re displaying a canvas and then creating panels and frames outside of that?

Cheers,

Kev

Thanks a lot !

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferStrategy;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JPanel;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author FEDERICA
 */
public class GameView extends Canvas {
    private BufferStrategy strategy;
    private boolean gameRunning=true;
    private Room room = new Room();
    private RunnerEntity runner;
    
public GameView(){
    //System.out.println("hey");
    JFrame container= new JFrame("Loderunner");
    JPanel panel= (JPanel) container.getContentPane();
    panel.setPreferredSize(new Dimension(460,460));
    panel.setMinimumSize(new Dimension(460,460));
    panel.setLayout(null);
    setBounds(0,0,460,460);
    panel.add(this);
    setIgnoreRepaint(true);
    requestFocus();
    addKeyListener(new KeyHandle());
    container.pack();
    container.setVisible(true);
    container.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    createBufferStrategy(2);
    strategy=getBufferStrategy();
    loadLevel("level.txt");
}

public Room getRoomGame(){
    return room;
}

public void loadLevel (String ref){
    try{
	    
	    File file = new File(ref);
            Scanner load = new Scanner(file);
	    for(int i=0;i<20;i++){
		 for(int j=0;j<20;j++){
	         int n= load.nextInt();
                 if(n==0) room.getList().add(new Block("tileset_green.png",j*23,i*23));
                 if(n==1) room.getList2().add(new Block("tileset_ladder.png",j*23,i*23));
	         if(n==2) runner= new RunnerEntity("runner.png",j*23,i*23-13);//for now
                 }
	    }
	 }catch(FileNotFoundException e){ System.out.println("Error");}
}

public void draw(Graphics2D g){
   room.draw(g); 
   runner.draw(g);
}

public void move(){
    runner.move();
}

public void gameLoop(){
    while(true){
        Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
        g.setColor(Color.WHITE);
        g.fillRect(0,0,460,460);
        move();
        draw(g);
        g.dispose();
        strategy.show();
        try{Thread.sleep(10); } catch (Exception e) {}
    }
}

public static void main (String args[]){
    GameView g = new GameView();
    g.gameLoop();
}

private class KeyHandle extends KeyAdapter {
    
    public void KeyPressed(KeyEvent e){
    System.out.println("hey");
    if(e.getKeyCode()==KeyEvent.VK_G){
        System.out.println("hey");
        runner.setRunning(true);
        runner.setDX(1);
    }
        
    }
    
    public void KeyReleased(KeyEvent e){
    
    if(e.getKeyCode()==KeyEvent.VK_LEFT){
        runner.setRunning(false);
        runner.setDX(0);
    }
    }
        
    }
    
}

You have typos in your key listener:


		public void KeyPressed(KeyEvent e) {

Should be:


		public void keyPressed(KeyEvent e) {

Note the lower case “k”

Cheers,

Kev

No it’s ok … I have find the problem the code work only if i click into a windows… I don’t know why…

Try adding the same keylistener to both the container and the canvas.


KeyHandle handler = new KeyHandle();

this.addKeyListener(handler);
container.addKeyListener(handler);

Yes now work … Thanks at all …