I would like to create an application wide keyboard shortcut for a Java Swing application. Looping over all components and adding the shortcut on each, has focus related side effects, and seems like a brute force solution.
Anyone has a cleaner solution?
For each window, use JComponent.registerKeyboardAction
with a condition of WHEN_IN_FOCUSED_WINDOW
. Alternatively use:
JComponent.getInputMap(WHEN_IN_FOCUSED_WINDOW).put(keyStroke, command);
JComponent.getActionMap().put(command,action);
as described in the registerKeyboardAction API docs.