在GWT中单击ESCAPE(ESC)时关闭Dialogbox

时间:2010-11-29 08:09:09

标签: gwt keylistener dialog

如何在对话框中添加类似addCloseHandler的内容,以便在单击ESC键时关闭?

2 个答案:

答案 0 :(得分:37)

您可以覆盖onPreviewNativeEvent()类的DialogBox方法:

public void onModuleLoad() {
    ExtendedDialogBox dialog = new ExtendedDialogBox();

    dialog.add(new Label("some content"));

    dialog.show();
}

private class ExtendedDialogBox extends DialogBox {

    @Override
    protected void onPreviewNativeEvent(NativePreviewEvent event) {
        super.onPreviewNativeEvent(event);
        switch (event.getTypeInt()) {
            case Event.ONKEYDOWN:
                if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ESCAPE) {
                    hide();
                }
                break;
        }
    }
}

答案 1 :(得分:1)

@Override
 public boolean onKeyDownPreview(char key, int modifiers) {
     switch (key) {
       case KeyCodes.KEY_ESCAPE:
         hide();
         break;
     }

     return true;
 }