1

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());
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
John Smith
  • 231
  • 3
  • 12
  • Perhaps this may help you. http://stackoverflow.com/questions/5344823/how-can-i-listen-for-key-presses-within-java-swing-accross-all-components ? – r0ast3d Oct 22 '12 at 16:15
  • I can't seem to get it to work. How could I fit it in to this example here? – John Smith Oct 22 '12 at 16:47

1 Answers1

1

Try this code. After calling setFullScreenMode(), just add these two lines. When we enter full screen something goes wrong,

frame.setVisible(false);