You could try using JComponent#getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
From the Java Docs
Constant used for registerKeyboardAction that means that the command
should be invoked when the receiving component is in the window that
has the focus or is itself the focused component.