elkolotfi
elkolotfi

Reputation: 6170

SWT Pressing ENTER Button event handler

I'm trying to handle the event of pressing "enter button" when typing in a SWT Text field. Here is what I've done so far:

textField.addKeyListener(new KeyListener() {

        @Override
        public void keyReleased(KeyEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void keyPressed(KeyEvent arg0) {
            if(arg0.keyCode == SWT.CR) {
                //do something here....
            }
        }
    });

This actually works when I hit Enter button, but not when I hit the enter button on the numpad.

Someone knows the constant variable for this button?

Thanks

Upvotes: 2

Views: 3102

Answers (2)

greg-449
greg-449

Reputation: 111142

Try using a traverse listener which is intended for handling things like Enter:

textField.addTraverseListener(new TraverseListener()
  {
    @Override
    public void keyTraversed(final TraverseEvent event)
    {
      if (event.detail == SWT.TRAVERSE_RETURN)
        { 
          ...
        }
    }
  });

Upvotes: 2

Andrea
Andrea

Reputation: 6125

I would try with:

if (arg0.keyCode==SWT.CR || arg0.keyCode==SWT.KEYPAD_CR)

Upvotes: 3

Related Questions