如何在SWT(Shift + Left MouseUp)中一次监听键和鼠标事件?

时间:2014-04-07 10:48:04

标签: java swt

我想在SWT中捕获Shift + Left Mouse Up事件。我只想在按住Shift键并释放鼠标左键的情况下仅打印一些示例消息

1 个答案:

答案 0 :(得分:4)

倾听SWT.MouseUp并将Event.stateMaskSWT.SHIFT进行比较,然后将Event.button1进行比较。

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();
}