JFrame KeyListener Problem

Hi, My KeyListner is not working for my JFrame and JPanel and I can’t work out why.
I am using the exact code that has worked for me before
Please help!
Here is the JFrame


public Display() {
		setSize(640, 480);
		setTitle("Ludum Dare 23: ");
		setVisible(true);
		setResizable(false);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setLocationRelativeTo(null);
		createBufferStrategy(2);
		Game game = new Game(this.getBufferStrategy());
		setFocusable(true);
		addKeyListener(new InputManager());
		add(game);
	}

Max

Anyone? (Please note this is for the Ludum Dare! so I kind of need it fast! :slight_smile: )
Also I tried adding Mouse listener and it didnt work?

Usually we add it to container (JPanel or Canvas). More since its KeyListener problem you should post your InputManager class. I’m in LD23 too.


public class InputManager extends KeyAdapter{

	public void keyPressed(KeyEvent key) {
		System.out.println(key.getKeyChar());
	}

	public void keyReleased(KeyEvent key) {
		System.out.println(key.getKeyChar());
		
	}

	public void keyTyped(KeyEvent key) {
		System.out.println(key.getKeyChar());
		
	}
}

This is just to test it

Hmm nothing wrong, except InputManager class isn’t common to be public since we place it to the class that need it so it can access game resource from that class.

Wait, you said JFrame but your method show that like you extend Canvas.

My JFrame Doesnt extends canvas?
Is that what you mean?

Post full code that extend JFrame.


package uk.co.maxkingstudios.ld23;

import javax.swing.JFrame;

public class Display extends JFrame {

	private static final long serialVersionUID = 1L;

	public Display() {
		setSize(640, 480);
		setTitle("Ludum Dare 23: ");
		setVisible(true);
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLocationRelativeTo(null);
		createBufferStrategy(2);
		Game game = new Game(this.getBufferStrategy());
		add(game);
	}

	public static void main(String[] args) {
		new Display();
	}
}

This is the JFrame class

Hmm weird since nothing wrong. I’ll share you mine (extending applet)


canvas.setPreferredSize(new Dimension(WIDTH, HEIGHT));
		canvas.setBounds(0, 0, WIDTH, HEIGHT);
		canvas.setIgnoreRepaint(true);
		this.add(canvas);
		addKeyListener(new KeyInput());
		canvas.createBufferStrategy(2);
		strategy = canvas.getBufferStrategy();

This’s used on my previous jam.

You have to do this even though you extend JFrame:


public Display(JFrame frame){
     // Other stuff
     frame.addKeyListener(new InputHandler());
}

public static void main(String[] args){
     new Display(this);
}

Try requestFocus().


public Display() {
      setSize(640, 480);
      setTitle("Ludum Dare 23: ");
      setResizable(false);
      setDefaultCloseOperation(3); // 3 == EXIT_ON_CLOSE
      createBufferStrategy(2);
      Game game = new Game(this.getBufferStrategy());
      setFocusable(true); // setfocusable before adding any listeners
      addKeyListener(new KL()); // Adding the KeyListener/Adapter created below
      requestFocus(); // request initial focus
      add(game);
      pack(); // final frame init
      setVisible(true); // frames visible after all is added
      setLocationRelativeTo(null); // Move frame to center after visible
}

// New inner class that handles KeyEvents
public class KL extends KeyAdapter {
      public void keyPressed(KeyEvent e) { 
      }
      public void keyReleased(KeyEvent e) { 
      }
      public void keyTyped(KeyEvent e) { 
      }
}

Just realized you already had a class of a KeyAdapter, oops.

You have to add the listeners on the component that you draw on, not the JFrame

That’s what I said first.