[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Newsgroup Home]
[news.eclipse.platform.swt] Re: Key-events on Carbon and on GTK

Dear Grant

Thanks a lot for the explanation. I was not aware that KeyDown was more commen. I'm certainly going to change it.

Peter

Grant Gayed schrieb:
The platform difference you're seeing is that on OSX, when you press Escape,
its KeyDown is used to close the sub dialog, and its KeyUp is delivered to
the new active shell (the second dialog), which gives it to its focus
control (the Text), which calls close().  On win32 the KeyUp does not get
delivered to your Text.  Is there any reason that the listener on your Text
is listening on KeyUp instead of KeyDown?  Listening on KeyDown is much more
common and less prone to issues like this.

Grant


"Peter Kullmann" <p.kullmann@xxxxxxxxx> wrote in message news:g2m5gn$dgk$1@xxxxxxxxxxxxxxxxxxxx
In one of our applications we have a situation where the user works in a
dialog. On some action a second modal dialog is displayed. When the user
dismisses this dialog in the foreground with the escape key she gets
back to the first dialog. The strange thing is that under OS-X and under
Linux the first dialog gets closed too. It's like the user pressed the
escape key twice.

Below is a snippet that demonstrates the issue. Open the sub dialog and
press the escape key. Both dialogs are then closed.

- This happens on 3.3 and on 3.4
- One dialog has a Text control and adds traverse and key listeners. If
one omits these listeners, the code works as expected. But, of course,
these listeneres are there for a reason which is no longer apparent in
this smaller example.
- The snippet uses some JFace code, but this is only to simplify the
example, I believe.
- If I add a breakpoint to the close method of the sub-dialog and wait a
fews seconds before I continue, everything works as expected.

Thanks for any input
Peter


package org.eclipse.jface.snippets;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.TraverseEvent;
import org.eclipse.swt.events.TraverseListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class TestEscape {
     private static final class MyDialog extends Dialog {
         private MyDialog(Shell parentShell) {
             super(parentShell);
         }

         @Override
         protected Control createDialogArea(Composite parent) {
             Composite composite = (Composite)
super.createDialogArea(parent);
             Text text = new Text(composite, SWT.NONE);
             text.addTraverseListener(new TraverseListener() {
                 public void keyTraversed(TraverseEvent e) {
                     if (e.detail == SWT.TRAVERSE_ESCAPE) {
                         e.doit = false;
                     }
                 }
             });
             text.addKeyListener(new KeyAdapter() {
                 @Override
                 public void keyReleased(KeyEvent e) {
                     if (e.character == '\u001b') { // Escape
                         close();
                     }
                 }
             });
             Button b = new Button(composite, SWT.PUSH);
             b.setText("Open sub dialog");
             b.addSelectionListener(new SelectionAdapter() {
                 @Override
                 public void widgetSelected(SelectionEvent e) {
                     Dialog subDlg = new Dialog(getShell()) {
                     };
                     subDlg.open();
                 }
             });
             return composite;
         }
     }

     public static void main(String[] args) {
         Display display = new Display();
         final Shell shell = new Shell(display);
         shell.setLayout(new FillLayout());
         Button b = new Button(shell, SWT.PUSH);
         b.setText("Open dialog");
         b.addSelectionListener(new SelectionAdapter() {
             @Override
             public void widgetSelected(SelectionEvent e) {
                 final Dialog dialog = new MyDialog(shell);
                 dialog.open();
             }
         });
         shell.pack();
         shell.open();

         while (!shell.isDisposed()) {
             if (!display.readAndDispatch()) {
                 display.sleep();
             }
         }

         display.dispose();
     }

}