swing vs keylistener

Hello.
So I have a game menu and to get out fast from game I put keylistener and if ESC is pressed game shuts down. Problem is for creating game, joining and such I’ve put swing components and when you are entering data or you finished entering data focus stays on swing components so my keylistener can’t register when ESC is pressed (swing keylistener is in charge then). Is there a quick and painless solution for this? When typing game name and such nobody uses ESC… can I make my keylistener work in parallel with swing’s?

Hi,

You can do that by adding ESC to the list of keys listened to by the swing components by default - like ‘TAB’ (change focus) and ‘SPACE’ (activate), which always work for any swing component. Sorry I can’t remember how to do it exactly, just look up the API docs to find out how these keys are always listened to.

You could add an EventListener directly to the AWTEvent queue.


  Toolkit.getDefaultToolkit().addAWTEventListener(
     new AWTEventListenereventListener()
     {
        public void eventDispatched(AWTEvent event)
        {
           // Implement your key listenening here
        }
     },
     0xffffffff   // This gets all events. You should mask out the ones you don't need
   );

See the API documentation for details.

In my games, I add the keylistener to every child component of the component I want to listen to, as well as to the component itself.

tnx guys, I think I’ll try Keith’s advice first as it seems most OOP… but tnx for all other solutions, always nice to know other ways to do stuff.
Writing awt listener seems to deep, rough, not java like, also as adding listeners to many components.