Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8144803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:28:46+00:00 2026-06-06T13:28:46+00:00

I’m having problems with getting my KeyBoardFocusManger to work with my full screen Window

  • 0

I’m having problems with getting my KeyBoardFocusManger to work with my full screen Window. No matter what, it just wont get keyboard input. I used a System.exit(0) and a println() to look for any call to the keypressed/released/typed method, but no errors are thrown. I’ve tried KeyListeners; but after I read this, I changed to a KeyboardFocusManager, and the same thing still happens. I’m really getting desperate; from what I can judge, the Window is not getting focus of the keyboard?

Here is my main:

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {
            // Determine if full-screen mode is supported directly
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            GraphicsDevice gs = ge.getDefaultScreenDevice();
            if (gs.isFullScreenSupported()) {
                Frame frame = new Frame(gs.getDefaultConfiguration());
                SpaceInvaderUI spaceInvaderUI = new SpaceInvaderUI(frame);
                // Enter full-screen mode
                gs.setFullScreenWindow(spaceInvaderUI);
            } else {
                JOptionPane.showMessageDialog(null, "Does not support full screen!", "Error 0x01", JOptionPane.ERROR_MESSAGE);
                System.exit(1);
            }
        }
    });
}

and here is the UI which contains the KeyBoardFocusManger, and is added in addListeners() method:

class SpaceInvaderUI extends Window {

    private JPanel drawingPanel;
    private Image background;
    private JButton btnExit;

    public SpaceInvaderUI(Frame frame) {
        super(frame);
        try {
            background = ImageIO.read(getClass().getResourceAsStream("background.png"));
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Could not extract resource: " + ex.getMessage(), "Error 0x02", JOptionPane.ERROR_MESSAGE);
            System.exit(2);
        }
        createWindow();
    }

    private void createComponents() throws HeadlessException {
        drawingPanel = new DrawingPanel(background, this);
        btnExit = new JButton("Exit");
    }

    private void createWindow() {
        createComponents();
        addListeners();
        addComponentsToWindow();
    }

    private void addComponentsToWindow() {
        add(drawingPanel, BorderLayout.CENTER);
        add(btnExit, BorderLayout.SOUTH);
    }

    private void addListeners() {
        KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
        manager.addKeyEventDispatcher(new MyDispatcher());
        btnExit.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                System.exit(0);
            }
        });
    }

    private class MyDispatcher implements KeyEventDispatcher {

        @Override
        public boolean dispatchKeyEvent(KeyEvent e) {
            if (e.getID() == KeyEvent.KEY_PRESSED) {
                System.out.println("pressed");
                System.exit(0);
            } else if (e.getID() == KeyEvent.KEY_RELEASED) {
                System.out.println("released");
                System.exit(0);
            } else if (e.getID() == KeyEvent.KEY_TYPED) {
                System.out.println("Typed");
                System.exit(0);
            }
            return false;
        }
    }
}

The exit button is just because I got tired of killing my app via taskmanager. Finally here is my panel on which the game will take place and my background is painted on:

public class DrawingPanel extends JPanel {

    private final Image background;
    private final SpaceInvaderUI invaderUI;

    DrawingPanel(Image background, SpaceInvaderUI invaderUI) {
        this.background = background;
        this.invaderUI = invaderUI;
    }

    @Override
    protected void paintComponent(Graphics grphcs) {
        super.paintComponent(grphcs);
        grphcs.drawImage(background.getScaledInstance((int) invaderUI.getWidth(), (int) invaderUI.getHeight(), Image.SCALE_SMOOTH), 0, 0, this);
    }
}

Thank you in advance.

EDIT: I have now tried using a keybinding on my drawingPanel but still nothing happens when I press f2:

class SpaceInvaderUI extends Window {

    private JPanel drawingPanel;
    private Image background;
    private JButton btnExit;

    public SpaceInvaderUI(Frame frame) {
        super(frame);
        try {
            background = ImageIO.read(getClass().getResourceAsStream("background.png"));
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Could not extract resource: " + ex.getMessage(), "Error 0x02", JOptionPane.ERROR_MESSAGE);
            System.exit(2);
        }
        createWindow();
    }

    private void createComponents() throws HeadlessException {
        drawingPanel = new DrawingPanel(background, this);
        btnExit = new JButton("Exit");
    }

    private void createWindow() {
        createComponents();
        addListeners();
        addComponentsToWindow();
    }

    private void addComponentsToWindow() {
        add(drawingPanel, BorderLayout.CENTER);
        add(btnExit, BorderLayout.SOUTH);
    }

    private void addListeners() {
        Action exit = new AbstractAction() {

            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        };
        drawingPanel.getInputMap().put(KeyStroke.getKeyStroke("F2"),
                exit);
        btnExit.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                System.exit(0);
            }
        });
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T13:28:48+00:00Added an answer on June 6, 2026 at 1:28 pm

    Why are you using AWT components in your Swing GUI? I fear (but don’t know for sure) that by doing this, you may be losing some of the Swing functionality.

    If you are only capturing select key select key strokes to control the game, consider using Key Bindings.

    Edit:
    No, the AWT components aren’t at fault, but still probably should not be used.

    Edit 2:
    Your top level Window is not focused for some reason. Continuing to test code…

    Edit 3:
    Using a JFrame worked for me:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Test3 {
       public static void main(String[] args) {
          EventQueue.invokeLater(new Runnable() {
    
             @Override
             public void run() {
                GraphicsEnvironment ge = GraphicsEnvironment
                      .getLocalGraphicsEnvironment();
                GraphicsDevice gs = ge.getDefaultScreenDevice();
                if (gs.isFullScreenSupported()) {
                   SpaceInvaderUI spaceInvaderUI = new SpaceInvaderUI(gs.getDefaultConfiguration());
                   gs.setFullScreenWindow(spaceInvaderUI);
                } else {
                   JOptionPane.showMessageDialog(null,
                         "Does not support full screen!", "Error 0x01",
                         JOptionPane.ERROR_MESSAGE);
                   System.exit(1);
                }
             }
          });
       }
    }
    
    // class SpaceInvaderUI extends JWindow {
    class SpaceInvaderUI extends JFrame {
    
       private JPanel drawingPanel;
       private Image background;
       private JButton btnExit;
    
       public SpaceInvaderUI(GraphicsConfiguration gc) {
          super(gc);
          createWindow();
          addKeyBindings();
          setUndecorated(true);
       }
    
       private void addKeyBindings() {
          int condition = JPanel.WHEN_IN_FOCUSED_WINDOW;
          InputMap inputMap = drawingPanel.getInputMap(condition );
          ActionMap actionMap = drawingPanel.getActionMap();
    
          boolean released = false;
          KeyStroke upArrowKeyStrokePressed = KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, released );
          String upArrowPressed = "up arrow pressed";
          inputMap.put(upArrowKeyStrokePressed , upArrowPressed);
          actionMap.put(upArrowPressed, new AbstractAction() {
    
             @Override
             public void actionPerformed(ActionEvent arg0) {
                System.out.println("up arrow pressed");
             }
          });
    
          released = true;
          String upArrowReleased = "up arrow released";
          KeyStroke upArrowKeyStrokeReleased = KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, released );
          inputMap.put(upArrowKeyStrokeReleased , upArrowReleased);
          actionMap.put(upArrowReleased , new AbstractAction() {
    
             @Override
             public void actionPerformed(ActionEvent arg0) {
                System.out.println("up arrow released");
             }
          });
    
       }
    
       private void createComponents() throws HeadlessException {
          drawingPanel = new DrawingPanel(background, this);
          btnExit = new JButton("Exit");
       }
    
       private void createWindow() {
          createComponents();
          addListeners();
          addComponentsToWindow();
       }
    
       private void addComponentsToWindow() {
          add(drawingPanel, BorderLayout.CENTER);
          add(btnExit, BorderLayout.SOUTH);
       }
    
       private void addListeners() {
    //      KeyboardFocusManager manager = KeyboardFocusManager
    //            .getCurrentKeyboardFocusManager();
    //      manager.addKeyEventDispatcher(new MyDispatcher());
          btnExit.addActionListener(new ActionListener() {
    
             @Override
             public void actionPerformed(ActionEvent ae) {
                System.exit(0);
             }
          });
       }
    //
    //   private class MyDispatcher implements KeyEventDispatcher {
    //
    //      @Override
    //      public boolean dispatchKeyEvent(KeyEvent e) {
    //         System.out.println("in dispatch. KeyEvent := " + e);
    //         if (e.getID() == KeyEvent.KEY_PRESSED) {
    //            System.out.println("pressed");
    //            System.exit(0);
    //         } else if (e.getID() == KeyEvent.KEY_RELEASED) {
    //            System.out.println("released");
    //            System.exit(0);
    //         } else if (e.getID() == KeyEvent.KEY_TYPED) {
    //            System.out.println("Typed");
    //            System.exit(0);
    //         }
    //         return false;
    //      }
    //   }
    }
    
    class DrawingPanel extends JPanel {
    
       private final Image background;
       private final SpaceInvaderUI invaderUI;
    
       DrawingPanel(Image background, SpaceInvaderUI invaderUI) {
          this.background = background;
          this.invaderUI = invaderUI;
          setBackground(Color.pink);
       }
    
       @Override
       protected void paintComponent(Graphics grphcs) {
          super.paintComponent(grphcs);
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I need to clean up various Word 'smart' characters in user input, including but
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.