Hey,
I’ve just started to notice that whenever I click on anything other than my JFrame after starting my program for testing the key input is completely ignored, even if I click on the JFrame again. The KeyListener, which is below, is added to the JFrame at the start of the program. I don’t see any reason for the key listener to just stop working when I click on my canvas or JTextArea.
package valkryst.core;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
/**
* This class handles all keyboard input from the user.
*
* @author Valkryst
* --- Last Edit 23-Sep-2013
*/
public class InputHandler implements KeyListener {
private boolean[] isPressed = new boolean[128];
/**
* Checks if the specified key is currently pressed.
* @param x The keyEvent.getKeyCode() number of the key.
* @return Return whether the specified key is pressed or not.
*/
public boolean isKeyPressed(final int x) {
return isPressed[x];
}
/**
* Forces the specified key to be set as released.
* @param x The keyEvent.getKeyCode() number of the key.
*/
public void forceRelease(final int x) {
if (x < isPressed.length)
isPressed[x] = false;
}
/**
* When a key is pressed then set that key as pressed.
*/
public void keyPressed(final KeyEvent e) {
final int KEY_CODE = e.getKeyCode();
if (KEY_CODE < isPressed.length)
isPressed[KEY_CODE] = true;
}
/**
* When a key is released then set that key as released.
*/
public void keyReleased(final KeyEvent e) {
final int KEY_CODE = e.getKeyCode();
if (KEY_CODE < isPressed.length)
isPressed[KEY_CODE] = false;
}
public void keyTyped(final KeyEvent e) {
}
}
package valkryst.core.graphics;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.TextArea;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import valkryst.core.Audio;
import valkryst.core.InputHandler;
import valkryst.systems.Message;
/**
* @author Valkryst
* --- Last Edit 07-Oct-2013
*/
public class Frame extends JFrame{
private static final long serialVersionUID = -4612661261769049289L;
public final static int WIDTH = 300;
public final static int HEIGHT = 168; //
public final static int SCALE = 3;
public final static InputHandler INPUT = new InputHandler();
public Frame(final Screen screen) {
setResizable(true); //You can set this to true, the graphics scale up.
setTitle("Temp");
add(screen);
pack();
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter() { // Runs before the game exits.
public void windowClosing(WindowEvent e) {
Audio.shutdown();
System.exit(0);
}
});
addKeyListener(INPUT);
setLayout(new BorderLayout());
add(screen, BorderLayout.CENTER);
setLocationRelativeTo(null);
setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
setMinimumSize(new Dimension(WIDTH, HEIGHT));
requestFocus();
setFocusable(true);
TextArea textArea = new TextArea();
textArea.setEditable(false);
textArea.setFocusable(false);
textArea.setCursor(getCursor());
textArea.setPreferredSize(new Dimension(WIDTH, HEIGHT + (HEIGHT/2)));
textArea.setMinimumSize(new Dimension(50, 100));
textArea.setMaximumSize(new Dimension(300, 600));
textArea.setBackground(Color.WHITE);
Message.setTextArea(textArea);
add(textArea, BorderLayout.SOUTH);
pack();
setVisible(true);
screen.start();
}
}
Sorry for the terrible description of what’s going on, I’ll improve it when I get back from class. Thanks for any replies.