GXT:如何拦截 ctrl-f4

发布于 2024-12-01 04:50:37 字数 736 浏览 4 评论 0原文

我想拦截 Ctrl+F4 以便关闭 TabPanel 的选项卡,而不是关闭我的应用程序正在其中运行的浏览器选项卡。如果我单击以下代码,则以下代码有效首先在选项卡面板内部:

    Viewport v = new Viewport();
    v.setLayout(new FitLayout());

    v.add(panel);
    v.addListener(Events.KeyDown, new Listener<BaseEvent>() {
        public void handleEvent(BaseEvent be) {
            KeyEvent ce = (KeyEvent)be;
            if (ce.isControlKey()) {
                if (ce.getKeyCode() == 115) {
                    System.out.println(ce.getKeyCode() + " Ctrl-f4");
                    ce.preventDefault();
                    ce.stopEvent();
                }
            }
        };
    });

有趣的是,如果焦点位于 TabPanel 之外的某个位置(显然位于视口内部),则不会触发该事件。

有什么想法吗?

I'd like to intercept Ctrl+F4 in order to close tabs of a TabPanel rather then the browser tab my application is running in. The following code is works if I click inside the tab panel first:

    Viewport v = new Viewport();
    v.setLayout(new FitLayout());

    v.add(panel);
    v.addListener(Events.KeyDown, new Listener<BaseEvent>() {
        public void handleEvent(BaseEvent be) {
            KeyEvent ce = (KeyEvent)be;
            if (ce.isControlKey()) {
                if (ce.getKeyCode() == 115) {
                    System.out.println(ce.getKeyCode() + " Ctrl-f4");
                    ce.preventDefault();
                    ce.stopEvent();
                }
            }
        };
    });

The funny thing is that if the focus is somewhre outside the TabPanel (which is obviously located inside the Viewport) the event isn't fired.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

十年九夏 2024-12-08 04:50:37

ctrl-f4 是 Windows 特定的,因此您不应该依赖它。

相反,要捕获用户关闭窗口/选项卡,请使用 Window.ClosingHandler :

     Window.addWindowClosingHandler(new Window.ClosingHandler() {
        @Override
        public void onWindowClosing(ClosingEvent closingEvent) {
            closingEvent.setMessage("Closing? Really?")
        }
    });

这将显示一个浏览器对话框,其中包含您的消息和确认按钮。

ctrl-f4 is windows specific, so you should not rely on it.

Instead to catch user closing window/tab use Window.ClosingHandler:

     Window.addWindowClosingHandler(new Window.ClosingHandler() {
        @Override
        public void onWindowClosing(ClosingEvent closingEvent) {
            closingEvent.setMessage("Closing? Really?")
        }
    });

This will show a browser dialog with your message and confirm buttons.

路还长,别太狂 2024-12-08 04:50:37

您可以使用 ClosingHandler 拦截窗口关闭事件并提示用户确认。但是,您无法完全阻止窗口/选项卡关闭 - 浏览器将不允许这样做。您将必须选择不同的键盘快捷键。

选择快捷方式后,请使用 NativePreviewHandler 来检测它:

Event.addNativePreviewHandler(new NativePreviewHandler() {
  @Override
  public void onPreviewNativeEvent(NativePreviewEvent event) {
    if (event.getTypeInt() == Event.ONKEYDOWN) {
      NativeEvent ne = event.getNativeEvent();

      if (ne.getKeyCode() == 't' && ne.getCtrlKey()) {
        // ...
      }
    }
  }
});

You can use a ClosingHandler to intercept the window close event and prompt the user for confirmation. You cannot, however, completely block the window/tab from closing - the browser will not allow it. You're going to have to choose a different keyboard shortcut.

Once you choose your shortcut use a NativePreviewHandler to detect it:

Event.addNativePreviewHandler(new NativePreviewHandler() {
  @Override
  public void onPreviewNativeEvent(NativePreviewEvent event) {
    if (event.getTypeInt() == Event.ONKEYDOWN) {
      NativeEvent ne = event.getNativeEvent();

      if (ne.getKeyCode() == 't' && ne.getCtrlKey()) {
        // ...
      }
    }
  }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文