Domanda

I want to capture Shift+Left Mouse Up event in SWT. I just want to print some sample message only while holding shift key down and releasing the left mouse button.

È stato utile?

Soluzione

Listen for SWT.MouseUp and compare the Event.stateMask to SWT.SHIFT, then compare Event.button to 1.

public static void main(String[] args)
{
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("StackOverflow");
    shell.setLayout(new GridLayout());

    Text text = new Text(shell, SWT.BORDER);

    text.addListener(SWT.MouseUp, new Listener()
    {
        @Override
        public void handleEvent(Event e)
        {
            if ((e.stateMask & SWT.SHIFT) != 0 && e.button == 1)
            {
                System.out.println("shift pressed and left mouse clicked");
            }
        }
    });

    shell.pack();
    shell.open();

    shell.layout(true, true);

    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top