How to listen key and mouse event at a time in SWT(Shift+ Left MouseUp)?

StackOverflow https://stackoverflow.com/questions/22910226

  •  28-06-2023
  •  | 
  •  

문제

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