1
votes

I have build a game in Java in full screen mode, I use this code to go full screen:

this.setPreferredSize(new Dimension((int)screenSize.getWidth(), (int)screenSize.getHeight()));
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
        if(gd.isFullScreenSupported()) {
            this.setUndecorated(true);
            gd.setFullScreenWindow(this);
        }
        else {
            System.err.println("Full screen not supported!!");
            
        }

On some windows systems when I press the ESCAPE key the program will exit fullscreen mode and minimize. How do I block this "function"?

3

3 Answers

1
votes

Write your own key listener, and handle your keypress, the way you want to. You could look at this.

EDIT: this seems to explain it better.

1
votes

If this is using Swing then you should be using Key Bindings. Swing was designed to use KeyBindings, not KeyEvents.

1
votes

I found the problem, when I switch views, the program loses focus, so windows will handle the ESCAPE key event. I solved it by requesting the focus every time the view changes.

like this:

this.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentShown(ComponentEvent e) {
                View.this.requestFocusInWindow();
            }            
        });