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.

有帮助吗?

解决方案

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();
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top