32

My code:

@Override
public void onKeyPress(KeyPressEvent event)
{
    if (event.getCharCode() == KeyCodes.KEY_ENTER)
    {
        registerButton.click();
    }
}

This is attached to a TextBox, and it does fire when I press enter. event.getCharCode() is just zero, not 13. When I press tab, it's 0, and when I press escape, it's 0. Argh!

This was working properly yesterday, and something has changed somewhere else in the project to affect this - but I'm not sure what it could be. It really seems like no relevant changes have been made in the last day.

If instead I handle a KeyUpEvent, this works as expected.

I'm using GWT 2.1.0. Thanks for any ideas!

enrybo
  • 1,787
  • 1
  • 12
  • 20
Riley Lark
  • 20,660
  • 15
  • 80
  • 128
  • This part seems correct, could you add the rest of the code? – Michaël Nov 08 '10 at 18:16
  • 1
    To isolate the problem I created a sample app w/ the Eclipse plugin and added a KeyPressHandler to the TextBox it creates... same issue! I filed a bug at http://code.google.com/p/google-web-toolkit/issues/detail?id=5558&q=rileylark&colspec=ID%20Type%20Status%20Owner%20Milestone%20Summary%20Stars – Riley Lark Nov 08 '10 at 19:48

6 Answers6

37

the KeyPressHandler is used for example for the SHIFT, CTRL, ALT keys.

If you want to attach an event to another key you have to use KeyDownHandler.

nameField.addKeyDownHandler(new KeyDownHandler() {

    @Override
    public void onKeyDown(KeyDownEvent event) {
        if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
            Window.alert("hello");
        }

    }

});
Abdusalam Ben Haj
  • 5,343
  • 5
  • 31
  • 45
Michaël
  • 3,679
  • 7
  • 39
  • 64
  • 1
    This worked for me, thanks. I was using KeyPressEvent and it detected the "enter" key press correctly in IE and Chrome, but not in Firefox (it was returning "0"). But this solution worked for me. – Michael Oct 05 '11 at 17:23
  • Yeah, KeyPressEvent (and no getNativeKeyCode) work for me on internet explorer and chrome, but not firefox [42]. KeyDownHandler and getnativeKeyCode not necessary to get that far. – cellepo Nov 08 '15 at 23:40
  • But the Answer by user2015482 does work in FF. That should be the accepted answer! – cellepo Nov 08 '15 at 23:55
8

or you can try this

if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER) {
}
enrybo
  • 1,787
  • 1
  • 12
  • 20
  • This didn't work for me. I had to switch from `KeyPressHandler` to `KeyDownHandler` (IE and Chrome were working fine, but Firefox wasn't). – Michael Oct 05 '11 at 18:31
  • 2
    Despite being chosen by asker, this is not the most complete, correct answer to the question. Instead of using this, user the answer provided by @Michaël for cross browser functionality. – Mark Tielemans Jan 25 '13 at 10:51
4

KeyUpHandler should be used instead of KeyPresshandler.

newSymbolTextBox.addKeyUpHandler(new KeyUpHandler() {
    @Override
    public void onKeyUp(KeyUpEvent event) {
        if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
            addStock();
        }
    }
});
Michaël
  • 3,679
  • 7
  • 39
  • 64
  • This works and should be the accepted answer: It works on Firefox also. See also http://stackoverflow.com/a/5570608/1357094. – cellepo Nov 08 '15 at 23:54
2

They might change the behavior on FF. I'm using GWT 2.4.0, and Firefox 10. According to this comment, You should use something like below before they fix the problem:

@Override
public void onKeyPress(KeyPressEvent event) {
    int keyCode = event.getUnicodeCharCode();
    if (keyCode == 0) {
        // Probably Firefox
        keyCode = event.getNativeEvent().getKeyCode();
    }
    if (keyCode == KeyCodes.KEY_ENTER) {
        // Do something when Enter is pressed.
    }
}
Vin
  • 559
  • 2
  • 18
1

I got the same problem when updating from 2.0.4 to 2.2.1, so it seems to be related to the gwt code.

Part of the issue is that KeyPressedEvent represents a native (ie browser-specific) key press event. In the bug you filed on this issue, one of the comments says:

It is however expected that "escape" does not generate a KeyPressEvent (IE and WebKit behavior); you have to use KeyDown or KeyUp for those. Firefox (and Opera) unfortunately fires many more keypress events than others (IE and WebKit, which have the most sensible implementation, the one the W3C is about to standardize in DOM 3 Events, AFAICT), but in that case getCharCode() is 0 so you can safely ignore them. The one exception (there might be others) is the "enter" key. Key/char events in browsers are such a mess that GWT doesn't do much to homogenize things (at least for now).

Until this mess is sorted out, your best bet is to use the workaround with KeyDownHandler or KeyUpHandler.

bspoel
  • 1,567
  • 2
  • 13
  • 22
0

GWT 2.5:

public void onKeyPress(KeyPressEvent event) {
    if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER) {
        // Event
    }
}
Sentence
  • 192
  • 4
  • 10