KeyFocus from Menu to Game

I have a game made (fullscreen graphics) and im trying to link my menu’s to the game. Just for testing, at the moment ive made a simple swing gui window with a ‘play’ button. When the button is pressed, the game is supposed to start.
This works, and the game does start when i hit ‘play’ but i dont have keyboard focus because i cant press any keys. Ive tried using requestFocus() and all kinds of focusManager stuff and i cant get it.

Im assuming somone out there has done this (probably loads of you) so if one of you could help me, id appreciate it. Thanks.

grabFocus()? might work. Make sure you’re calling it on the button and not the thing holding the button.

Kev

so u mean, when my GUI runs my main game class, i should use the grabFocus() method on some componant in the game class? like frame or does it have to be a button?

I believe it has to be the button. Its been a while since I looked at this sorta thing :confused:

Kev

the problem is, there are no componants on my game screen, cause its just a fullscreen game. the only componants are in the GUI menu that starts the game. I need to set the focus to the fullscreen game window (like the frame) after the GUI menu has run the main game.

Ah ha, missed your point, the GUI isn’t visible when you’d like to press the button? You can’t do that :slight_smile: If its not visible it won’t get key focus.

Kev

ok, so your saying i cant simply close the GUI menu and open the fullscreen game, then pass keyboard focus to the fullscreen game?
How do people get round this then?

You can pass the keyboard focus to the fullscreen, but you can’t leave it on the button.

If you want keyinput from the fullscreen add a KeyListener to the (J)Frame that you use to go into full screen.

Kev

I create my full screen like this


JFrame frame = new JFrame("My Game");
            frame.setBounds(0,0,WIDTH,HEIGHT);

            display = new DisplayMode(1280, 1024, 32, 75);
            
        screen = new ScreenManager();        
        screen.setFullScreen(display, frame);
        
        window = screen.getFullScreenWindow();

and then i add the keyListener to window. This works fine if i take out the menu and simply start the game, and keyboard inputs work fine. It just doesnt work if i start the game using a gui menu, its as if the keyboard focus is still stuck with the GUI menu and hasnt been passed into the fullscreen game.

and calling frame.requestFocus() or frame.getContentPane().requestFocus() after that code, does not work?

ive tried both, neither lets keyboard inputs be received. Ive seen load of games on these forums that have menus, so how do they manage to pass keyboard focus to the main game, after the menu has gone?

Normally, the window is closed before fullscreen starts?

Kev

i have a seperate GUI class called Menu. Thats the class with the main() that i run. When i run it, it creates a GUI and displays it.
I hit the play button on that GUI and it sets the Menu gui visable(false), sets up an object of my main game class (called World) and calls the run method of World which creates the fullscreen game and runs it.

Heres the actionListener of the menu class


      public void actionPerformed (ActionEvent e)
      {

            if (e.getSource() == btn_play)
            {
                  frame.setVisible(false);

                  World world = new World();            
                  world.run();
            }
            else if (e.getSource() == btn_exit)
            {
                  System.exit(0);
            }
            
      }

As i said, World is my main game class that goes fullscreen and starts the actual game. Its not created until the actionListener above calls the run method.

Is World an extension of Thread? If so, you should call start() not run() otherwise you’ll be tying the AWT thread up.

Kev

No, World doesnt extend Thread, it extends JFrame.

ive been looking through this class in the API. Im assuming the solution is going to come from this class, cause its function is to deal with the keyboard focus. I just cant figure out any way to set the focus of the keyboard to another frame i.e something like frame.setKeyboardFocus(true);
would be nice!!!

http://www.doc.ic.ac.uk/csg/java/1.4docs/api/java/awt/KeyboardFocusManager.html

I think your problem is that you run your game in the awt thread (the keyboard listener). Then there can’t be any key input because your hogging the thread. Instead start the game in a new thread.

Thanks alot, i tried what u said and it works fine now. Dont know why i didnt just do that earleir :slight_smile: