当按下键时,GWT onPreviewNativeEvent运行2次

时间:2013-01-05 12:49:03

标签: java events gwt

当我按下ESC键时,我使用该事件显示消息,但它会显示警报两次。错误在哪里?

Event.addNativePreviewHandler(new Event.NativePreviewHandler() {
    @Override
    public void onPreviewNativeEvent(Event.NativePreviewEvent event) {
        NativeEvent nativeEvent = event.getNativeEvent();
            if( nativeEvent.getKeyCode() == 27){
            Windows.alert("HELLO");
        }
        }
    });

1 个答案:

答案 0 :(得分:5)

它会触发KeyDown和KeyUp事件。你应该这样做:

public void onPreviewNativeEvent(NativePreviewEvent event) {
    NativeEvent nativeEvent = event.getNativeEvent();
    if ("keydown".equals(nativeEvent.getType())) {
        if (nativeEvent.getKeyCode() == KeyCodes.KEY_ESCAPE) {
            Windows.alert("HELLO");
        }
    }
}