1

Eclipse plugin: how to make Combo to handle Enter key pressing? That is after entering some text user can press Enter, that will case some processing. The same processing can be started by Button "Run" click.

org.eclipse.swt.widgets.Combo

Baz
  • 36,440
  • 11
  • 68
  • 94
Paul Verest
  • 60,022
  • 51
  • 208
  • 332
  • There is .addModifyListener method for the combo http://stackoverflow.com/questions/11653247/can-i-detect-change-in-text-fields-in-swt But I can't see that I can detect Enter was pressed. – Paul Verest Jul 07 '13 at 11:09

1 Answers1

2

Just add a Listener for SWT.KeyUp and check if the entered character is equal to SWT.CR.

Here is some code:

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

Looks like this:

enter image description here

And will show this dialog when the user presses enter:

enter image description here

Baz
  • 36,440
  • 11
  • 68
  • 94