pivot-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Chris Bartlett <cbartlet...@gmail.com>
Subject Re: OS variation in key listeners
Date Tue, 22 Mar 2011 06:10:56 GMT
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 <bill.van.melle@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.
>

Mime
View raw message