SWT Shell KeyListener

1.) Purpose

Widget for quick viewing of OSX files. Basically, on a Mac, when you select a file and press the spacebar, you get a quick look at that file. I am trying to reproduce this functionality using SWT + JFace (mmmaybe JavaFX).

It will be Shellfor me.

Mac screenshot of the quick preview


2.) Specificity

  • The preview widget will be APPLICATION_MODAL Shellattached to StructuredViewer.

  • I do not want to recreate Shellevery time I open a preview. I just want it hidden. This preview is supposed to be quick .

  • I want to close this Shellon ESCand SPACE.

  • (PDF, JPEG, PNG, TXT ..), . Shell , .

  • , . , , - .


3.)

  • Shell , "" KeyEvent s. , Shell , .

  • Display. / , / Shell. , , RCP, ( - ).


4.) SSCCE

, , Text , Shell , .

/**
 * 
 * @author ggrec
 *
 */
public class SSCCE_ShellWithShellParent
{

    // ==================== 1. Static Fields ========================

    private static final int CHILD_SHELL_STYLE = SWT.BORDER | SWT.RESIZE | SWT.TITLE | SWT.CLOSE | SWT.APPLICATION_MODAL;


    // ==================== 3. Static Methods ====================

    public static void main(final String[] args)
    {
        new SSCCE_ShellWithShellParent();
    }


    // ==================== 4. Constructors ====================

    private SSCCE_ShellWithShellParent()
    {
        final Display display = new Display();
        final Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());

        final Shell childShell = new Shell(shell, CHILD_SHELL_STYLE);
        childShell.setLayout(new FillLayout());
        childShell.pack();

        createChildContents(childShell);

        final KeyAdapter keyListener = new KeyAdapter()
        {
            @Override public void keyPressed(final KeyEvent e)
            {
                final Object source = e.getSource();

                if (source == shell)
                    System.out.println(e.keyCode);
                else
                    System.err.println(e.keyCode);

                // Escape
                if (e.keyCode == 27)
                    shell.close();

                // Spacebar
                if (e.keyCode == 32 && e.getSource() == shell)
                    childShell.open();
            }
        };

        childShell.addKeyListener(keyListener);
        shell.addKeyListener(keyListener);

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


    // ==================== 5. Creators ====================

    private void createChildContents(final Shell childShell)
    {
        new Text(childShell, SWT.NULL);
    }

}

5.) TL; DR

KeyListener Shell .

+4
1

. :

  • WHATEVER_CHILD.setFocusable(false);

  • , , : YOUR_SHELL.addFocusListener(new FocusListener{ public void focusLost(FocusEvent e) { YOUR_SHELL.requestFocusOnWindow(); } });

.

0

Source: https://habr.com/ru/post/1546999/


All Articles