现在的位置: 首页 > 综合 > 正文

SWT的SelectionEvent.stateMask不起作用

2012年06月15日 ⁄ 综合 ⁄ 共 933字 ⁄ 字号 评论关闭

通常理解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();
 }
}

抱歉!评论已关闭.