开发者

GXT: How to intercept ctrl-f4

开发者 https://www.devze.com 2023-03-30 19:12 出处:网络
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:

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?


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.


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()) {
        // ...
      }
    }
  }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号