You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@pivot.apache.org by Bill van Melle <bi...@gmail.com> on 2011/03/22 02:20:00 UTC

OS variation in key listeners

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.

Re: OS variation in key listeners

Posted by Bill van Melle <bi...@gmail.com>.
>
> Did you try keyPressed() rather than keyTyped(), or a key other than
> 'Escape'?  (Don't bother spend time investigating just to answer).
>

No, didn't try any variations.


> Hopefully your use of Adapters has uncluttered your code a bit too!
>

Sure did!  They make such excellent sense.

Re: OS variation in key listeners

Posted by Chris Bartlett <cb...@gmail.com>.
Thanks for the update, Bill.  I'm sure it will be useful to others.

I'm not sure why the keyTyped() method doesn't get triggered on those OSs.
 Perhaps they are handling the AWT events differently somehow, or there is
some other Component in the focus hierarchy that is consuming the keyTyped
event before it reaches your listener method?  Without seeing your code it
is hard to know what might be happening.

Did you try keyPressed() rather than keyTyped(), or a key other than
'Escape'?  (Don't bother spend time investigating just to answer).

Hopefully your use of Adapters has uncluttered your code a bit too!

Chris

On 22 March 2011 08:20, Bill van Melle <bi...@gmail.com> wrote:

> 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.
>