当我按下ESC键时,我使用该事件显示消息,但它会显示警报两次。错误在哪里?
Event.addNativePreviewHandler(new Event.NativePreviewHandler() {
@Override
public void onPreviewNativeEvent(Event.NativePreviewEvent event) {
NativeEvent nativeEvent = event.getNativeEvent();
if( nativeEvent.getKeyCode() == 27){
Windows.alert("HELLO");
}
}
});
答案 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");
}
}
}