3

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

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

1 回答 1

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");
        }
    }
}
于 2013-01-05T13:23:29.463 回答