I'm trying to write a full screen application that takes keyboard inputs. I've tried adding keylisteners to my JFrame and now to a canvas within the JFrame and setting them both as focusable but it doesn't seem to be registering anything at all. Is there something special I have do to in full screen mode to get it to register my keyEvents? Any help greatly appreciated. (display is just an extension of JFrame with no changes yet)
public class chanceCore implements Runnable, KeyListener, MouseListener {
boolean isRunning = true;
display frame;
Canvas canvas;
GraphicsDevice device;
GraphicsEnvironment environment;
public chanceCore() {
setUp();
run();
}
public void setUp() {
frame = new display("CGE");
canvas = new Canvas();
frame.getContentPane().add(canvas);
frame.setUndecorated(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
canvas.addKeyListener(this);
canvas.addMouseListener(this);
canvas.setFocusable(true);
frame.setVisible(true);
environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
device = environment.getDefaultScreenDevice();
device.setFullScreenWindow(frame);
}
@Override
public void run() {
while (isRunning) {
draw(canvas.getGraphics());
try {
Thread.sleep(200);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
public void draw(Graphics g) {
g.setColor(Color.BLUE);
g.fillRect(0, 0, frame.getWidth(), frame.getHeight());
}
@Override
public void keyPressed(KeyEvent e) {
System.out.println(e.getKeyCode());
}