How to track mouse over SWT composite made from sub controls?

孤人 提交于 2019-12-13 13:15:12

问题


I have a created my own control:

I want to track the mouse and added a MouseTrackListener. Unfortunately MouseEnter and MouseLeave events are also generated, when the mouse moves over the parts of my composite (that is the label and the button).

[Mouse enter] - mouse enters the empty space
[Mouse hover] - mouse is over the empty space
[Mouse exit]  - mouse moved over label
[Mouse enter] - mouse leaves label and enters empty space
[Mouse hover] - mouse over empty space
[Mouse exit]  - mouse leaves composite

How can I track the composite as one complete thing instead of sub parts?

public class MyComposite extends Composite {
    public MyComposite(final Composite parent, final int style) {
        super(parent, style);

        final Label lbl = new Label(this, SWT.NONE);
        lbl.setBounds(10, 10, 78, 15);
        lbl.setText("My Composite");

        final Button btn = new Button(this, SWT.NONE);
        btn.setBounds(190, 29, 75, 25);
        btn.setText("Ok");

        pack();
    }

    public static void main(final String[] args) {
        final Shell shell = new Shell(Display.getDefault());
        shell.setText("Testcase");
        shell.setLayout(new FillLayout());

        final MyComposite comp = new MyComposite(shell, SWT.NONE);
        comp.addMouseTrackListener(new MouseTrackListener() {
            @Override
            public void mouseHover(final MouseEvent e) {
                System.out.println("[Mouse hover]");
            }

            @Override
            public void mouseExit(final MouseEvent e) {
                System.out.println("[Mouse exit]");
            }

            @Override
            public void mouseEnter(final MouseEvent e) {
                System.out.println("[Mouse enter]");
            }
        });

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

回答1:


If you want to get only mouse move events you can add a untyped event to Composite. It should be something like

final Composite comp = new Composite(shell, org.eclipse.swt.SWT.NONE);
comp.addListener(SWT.MouseMove, new Listener() {
    public void handleEvent(Event event) {
        System.out.println("hit");
    }
});

If you add the same Listener instance to all the children of MyComposite then you can capture all the mouse move events.

Alternately you can use Display.addFilter to catch all the mouse events and filter if the ones that happen on your Composite instance or any of its children.

A third option is to use Composite.setCapture to get all mouse events when mouse enters the Composite area and stop capturing when it leaves.

Out of these I think the first option is probably the best performant.



来源:https://stackoverflow.com/questions/16543662/how-to-track-mouse-over-swt-composite-made-from-sub-controls

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!