2013-07-07 35 views
1

Eclipse插件:如何让Combo处理Enter键按? 即输入一些文本后用户可以按回车,这样会处理一些情况。 按钮“运行”单击可以启动相同的处理。Eclipse插件:让Combo处理Enter键

org.eclipse.swt.widgets.Combo

+0

有梳子的.addModifyListener方法o http://stackoverflow.com/questions/11653247/can-i-detect-change-in-text-fields-in-swt但我看不到我可以检测到Enter被按下。 –

回答

1

只需添加一个ListenerSWT.KeyUp并检查输入的字符等于SWT.CR

下面是一些代码:

public static void main(String[] args) 
{ 
    Display display = new Display(); 
    final Shell shell = new Shell(display); 
    shell.setText("StackOverflow"); 
    shell.setLayout(new GridLayout(1, true)); 

    final Combo combo = new Combo(shell, SWT.NONE); 

    combo.addListener(SWT.KeyUp, new Listener() 
    { 
     @Override 
     public void handleEvent(Event arg0) 
     { 
      if(arg0.character == SWT.CR) 
       MessageDialog.openInformation(shell, "Input", "You entered: " + combo.getText()); 
     } 
    }); 

    shell.pack(); 
    shell.open(); 
    while (!shell.isDisposed()) 
    { 
     if (!display.readAndDispatch()) 
      display.sleep(); 
    } 
    display.dispose(); 
} 

是这样的:

enter image description here

并显示当用户按下输入此对话框:

enter image description here

相关问题