2011-08-23 61 views
0

我想攔截按Ctrl + F4 爲了關閉的TabPanel的標籤而不是我的應用程序運行在瀏覽器選項卡下面的代碼是作品,如果我單擊該選項卡面板第一內側:GXT:如何截取Ctrl-F4

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(這顯然是位於視口內)外somewhre事件不會觸發。

任何想法?

回答

0

您可以使用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()) { 
     // ... 
     } 
    } 
    } 
}); 
+0

現在趕上事件,停止它不行。我試過ne.stopPropagation(),ne.preventDefault()和event.cancel()。但沒有任何工作,即無論如何該選項卡被瀏覽器關閉。 – Simon

0

ctrl-f4是Windows特定,所以你不應該依賴於它。

相反趕上用戶關閉窗口/標籤使用Window.ClosingHandler

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

這將顯示您的郵件瀏覽器對話框,確認按鈕。

+0

你絕對正確:我也需要抓住其他組合。但是當我想在ctrl + f4(或ctrl + q或其他)被捕獲時關閉TabPanel中的面板。因此我不能使用關閉處理程序。 – Simon