But it's only working when none of the frame components is focused.
Correct. You are using the InputMap
for when the component has focus. If you want the binding to be active even when the component doesn't have focus you need to use a different InputMap
. Read the section from the Swing tutorial How Key Bindings Work for the 3 different InputMap types.
How to make key bindings for java.awt.Frame and for all its components?
Key Bindings work on Swing components, not AWT components.
You should add the bindings to the JRootPane
of the JFrame
:
frame.getRootPane().getInputMap(...)...
frame.getRootPane().getActionMap()...