The window's not losing focus, the button is gaining focus, taking it away from your component with the KeyListener
.
In your investigations, you should have also found out that KeyListener
will only respond to key events when the component is focusable AND has focus. This is a known limitation of KeyListener
...
Also in your investigations, you should have found that Key Bindings API is normally recommended as fix to this problem...