My key bindings don't work...
Why would key bindings work with the WHEN_IN_FOCUSED_WINDOW input map, but not the WHEN_FOCUSED input map? The component is focused. There is a key listener on it, and it works. (The key listener does not consume events that have any modifiers other than shift.) I even called setFocusable() on the component to make sure it can get focus. I don't think the actions are the problem; the same actions are in a popup menu shown by a mouse listener on the component, and they work when activated from there.
The component is a JViewport, in case that matters somehow.
// this does not work
viewport.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK), "copy");
viewport.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK), "paste");
Re: My key bindings don't work...
I found the problem. I was misreading my own code. (Funny what you don't see when you think you know what's there.) The key listener was not on the viewport; it was on the scroll pane itself. More importantly, the scroll pane actually had the focus. I changed the key listener and the focus (set by a parent container) to the viewport instead of the scroll pane, and everything works as intended.