通常理解stateMask的作用是标志事件发生时是否有Shift、Ctrl等键同时按下,但SWT里绝大多数Control都不支持这个标志,无论按下什么键,事件对象里的stateMask都是0(不信可以运行下面的代码)。更奇怪的是这个问题已经被提出3年多还没有解决,stateMask只对MenuItem有用,那要怎么实现对Button的Ctrl+Click检测呢?
import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class ButtonTest { public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout());
shell.setSize(200, 200); final Button button = new Button(shell, SWT.NONE);
button.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) {
System.out.println(e.stateMask);//Always zero
}
});
button.setText("Test");
shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
搬家前链接:https://www.cnblogs.com/bjzhanghao/archive/2007/08/28/873677.html
欢迎转载
请保留原始链接:https://bjzhanghao.com/p/2694
请保留原始链接:https://bjzhanghao.com/p/2694