Unresponsive Keylistener for Jframe

Unresponsive KeyListener for JFrame

You must add your keyListener to every component that you need. Only the component with the focus will send these events. For instance, if you have only one TextBox in your JFrame, that TextBox has the focus. So you must add a KeyListener to this component as well.

The process is the same:

myComponent.addKeyListener(new KeyListener ...);

Note: Some components aren't focusable like JLabel.

For setting them to focusable you need to:

myComponent.setFocusable(true);

JFrame not responding to keylistener

ah yeah I see now :) In that case it's a bit more involved, you need to use application wide actions, or well an application-wide key listener, because KeyListeners won't work on containers when a child component has the focus... See this question

Setting up application wide Key Listeners

edit: sure there's always a quick and dirty fix :p

try this:

public class Test2 extends JFrame {

private boolean $isPressed; /*
* To check if we already listened to a key
* press event
*/

/**
* Constructor to make a new quiz
*/
public Test2() {

this.setTitle("");

$isPressed = false;

setFocusable(true);
getContentPane().setLayout(new BorderLayout());
JButton b = new JButton();
b.addFocusListener(new FocusListener() {

@Override
public void focusLost(FocusEvent e) {
// TODO Auto-generated method stub

}

@Override
public void focusGained(FocusEvent e) {
Test2.this.requestFocus();
}
});
getContentPane().add(b);
/* Add a keylistener for every team */
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {

System.out.println("TEST");
}
});
$isPressed = false;
b.grabFocus();
pack();
}

/**
* @param args
*/
public static void main(String[] args) {
new Test2().setVisible(true);

}
}

so essentially you can add a focus listener to your components.. but then for this particular hack you'll need to do it for all your child components... obviously you'd only need to define one FocusListener and you can reuse it everywhere, but generally there's a reason for why a component has focus so you don't want to just make it lose focus.. but maybe in your case it doesn't matter.

so just to clarify, without the FocusListener on the JButton above, "TEST" gets printed in the console until I click on the button, then it no longer works. Then with the FocusListener, it will always work because the JFrame will regain focus when the button has the focus, so the KeyListener on it will work again.

Another way to solve this problem would be to define one instance of the KeyAdapter, and set it as KeyListener to all your components in that window.

KeyListener not working with JFrame Canvas

You have to add a KeyListener to your Canvas to be able to receive input from it, e.g.

canvas.add(new Keyboard());

KeyListener on JPanel randomly unresponsive

Try adding a JButton to your "canvas" JPanel, then pressing the button and seeing what happens to your KeyListener -- it fails because the JPanel lost the focus. To prevent this from happening, use Key Bindings instead (see the link in my comment above for the tutorial). For e.g.,

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

@SuppressWarnings("serial")
public class Game2 {

private static final String UP = "up";

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

public Game2() {
JFrame window = new JFrame("Press up-arrow key");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel canvas = new JPanel();
canvas.setPreferredSize(new Dimension(400, 300));
window.add(canvas);

canvas.add(new JButton(new AbstractAction("Press space-bar") {
public void actionPerformed(ActionEvent e) {
System.out.println("Button or space-bar pressed");
}
}));
ActionMap actionMap = canvas.getActionMap();
int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;
InputMap inputMap = canvas.getInputMap(condition);

inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), UP);
actionMap.put(UP, new UpAction());

window.pack();
window.setLocationRelativeTo(null);
window.setVisible(true);
}
}

@SuppressWarnings("serial")
class UpAction extends AbstractAction {
@Override
public void actionPerformed(ActionEvent arg0) {
System.out.println("Up Arrow pressed!");
}
}

keyListener on JFrame not reacting

KeyListeners don't work on containers when a child component has the focus. You will need an application wide KeyListener. Check out this SO question:

Setting up application wide Key Listeners

KeyListener won't do its listening

So, I figured out the issue. You have to make sure that you add your KeyListener inside your JFrame, and not your JPanel. I'm not completely sure if this has to do with the fact that I extend JFrame in another class, but adding the KeyListener to the JPanel does not work (through the implements nor through adding it explicitly). As a result, you need to the KeyListener to the JFrame that holds the JPanel. I'm not entirely sure why I need to do this (my friends' games don't need this), but it's a fix.

Inside my GamePanel, I have a reference to the JFrame that holds the GamePanel (given as parameter to its constructor).

public class GamePanel {

public GamePanel(GamePanel Parent, WelcomePanel Sister) {
this.parent = Parent; // Parent JFrame
this.sister = Sister; // Sister JPanel that works with this JPanel

this.parent.addKeyListener(this); // do this

Do the above, instead of:

public GamePanel(GamePanel Parent, WelcomePanel Sister) {
addKeyListener(this);

This solved my issue. Thanks to all that helped! :)

KeyListener not responding to key inputs

as MadProgrammer has pointed out the right thing to do is use the Key Bindings API which you can see step by step Key Bindings API

add a constructor that adds a key listener and in your main make it class Game instead of JFrame

public class Game extends JFrame implements KeyListener{
public static int ppx,ppy;

public Game(String string) {
addKeyListener(this);
// TODO Auto-generated constructor stub
}

public static void main(String[] args) {
Game frame = new Game("Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,600);
frame.add(new THIng());

frame.setVisible(true);

}

@Override
public void keyTyped(KeyEvent e) {
}

@Override
public void keyPressed(KeyEvent e) {
int keycode = e.getKeyCode();
if(keycode == KeyEvent.VK_D){
System.out.println("Debug");
}
}

@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub

}

}


Related Topics



Leave a reply



Submit