This is a followup to that thread (
http://apache-pivot-users.399431.n3.nabble.com/KeyListeners-on-ImageView-td2600622.html)
people thought was getting too long.
I followed Chris's advice about making Window focusable -- create a trivial
class
public class FocusableWindowSkin extends WindowSkin {
@Override
public boolean isFocusable() {
return true;
}
}
and install it in my app's startup method
Theme.getTheme().set(Window.class, FocusableWindowSkin.class);
Then when I want to create a brand new OS window
with DesktopApplicationContext.createDisplay, I tell the Pivot Window that I
put in the OS window to requestFocus(), which now succeeds. After that it's
just a matter of handling key strokes.
On Windows XP, the following works for me to intercept the Escape key as a
shortcut for closing the window:
this.getComponentKeyListeners().add(new ComponentKeyListener.Adapter() {
@Override
public boolean keyTyped(Component component, char character) {
if (character == Keyboard.KeyCode.ESCAPE) {
closeHostWindow();
return true;
}
return false;
}
});
(where closeHostWindow does this.getDisplay().getHostWindow().dispose(); --
is that the right thing?)
However, on Mac OS X and Ubuntu Linux, it doesn't work. At first I thought
it might be something about key codes, but I set a breakpoint, and keyTyped
doesn't get called at all. The following alternative *does* work on all 3
OS's:
this.getActionMappings().add(new ActionMapping(
new Keyboard.KeyStroke(Keyboard.KeyCode.ESCAPE, 0),
new Action() {
@Override
public void perform(Component source) {
closeHostWindow();
}
}));
Although I'm vaguely curious why this should be so, I'm mainly putting this
out as a FYI for anyone searching on this topic.