﻿# 如何停用 Google Chrome 內建的 Ctrl-W 關閉頁籤快速鍵

昨天試用了一下 [GitHub Codespaces](https://github.com/features/codespaces/) 這套完全線上使用的整合開發環境，這套基於 Visual Studio Code 的雲端版本功能相當完整，所有在地端 Visual Studio Code 的設定，都可以全自動同步上去，所有擴充套件也都可以用。但唯一的不便之處，就是 Chrome 預設的 `Ctrl-W` (關閉目前頁籤) 快速鍵，會自動覆蓋掉 Visual Studio Code 常用的 `Ctrl-W` (關閉目前檔案)，可是這個快速鍵又這麼常用，所以一不小心就會關閉線上編輯器。因此，我研究出兩種方法有效避免頁籤被關閉！

### 透過 Tampermonkey 擴充套件

這個做法是透過 [Tampermonkey](https://chrome.google.com/webstore/detail/tampermonkey/dhdgffkkebhmkfjojejmpbldmpobfkfo) 擴充套件的 Userscript 使用者腳本指令碼，替特定網頁加入一段 JavaScript 程式碼，避免網頁被意外關閉。我個人比較喜歡這種做法，不用調整操作習慣。

1.  安裝 [Tampermonkey](https://chrome.google.com/webstore/detail/tampermonkey/dhdgffkkebhmkfjojejmpbldmpobfkfo) 擴充套件
    
2.  安裝我自己寫的 Userscript 使用者腳本指令碼
    
    點擊連結後會自動跳出 Tampermonkey 的安裝畫面，點擊「安裝」即可自動安裝完畢：
    
    [https://github.com/doggy8088/AvoidClosingTab/raw/master/AvoidClosingTab.user.js](https://github.com/doggy8088/AvoidClosingTab/raw/master/AvoidClosingTab.user.js)
    

### 透過 Better Ctrl-W 擴充套件

這個做法是透過 [Better Ctrl-W](https://chrome.google.com/webstore/detail/better-ctrl-w/jfknaapblnppeflfamkegnnonlfgokhp) 將 Chrome 內建的 `Ctrl-W` 快速鍵取消作用。

1.  安裝 [Better Ctrl-W](https://chrome.google.com/webstore/detail/better-ctrl-w/jfknaapblnppeflfamkegnnonlfgokhp) 擴充套件
    
2.  開啟 Chrome **擴充功能**的「鍵盤快速鍵」頁面
    
    ```bash
    chrome://extensions/shortcuts
    ```
    
3.  重新綁定快速鍵
    
    請將 `Ctrl-W` 綁定到 **Do absolutely nothing** 即可停用 Chrome 預設 `Ctrl-W` 快速鍵！
    
    再將 `Alt-W` 綁定到 **Close highlighted tabs** 即可用來取代原本關閉頁籤的快速鍵！
    
    ![將  綁定到 Do absolutely nothing 即可停用 Chrome 預設  快速鍵！](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/e3e05a4b6e38ded92ccd-94521801-350c1680-0261-11eb-9ac3-00a39cd6523d.jpg)
    
4.  注意事項
    
    1.  當你設定好之後，網頁中就再也抓不到任何 `Ctrl-W` 快速鍵了喔！
    2.  這擴充套件還無法針對**特定網站**停用 `Ctrl-W` 快速鍵，但已經看到有 [Issue](https://github.com/thalesmello/better-ctrlw/issues/3) 被提出，希望未來可以加上，畢竟並非所有網站都需要特別關閉 `Ctrl-W` 快速鍵！

### 相關連結

-   [Tampermonkey](https://chrome.google.com/webstore/detail/tampermonkey/dhdgffkkebhmkfjojejmpbldmpobfkfo)
    -   [Avoid Closing Tab](https://github.com/doggy8088/AvoidClosingTab)
-   [Better Ctrl-W - Chrome 線上應用程式商店](https://chrome.google.com/webstore/detail/better-ctrl-w/jfknaapblnppeflfamkegnnonlfgokhp)
    -   [Better Ctrl-W](https://github.com/thalesmello/better-ctrlw) - Don't close tabs with Ctrl by mistake anymore
    -   [remote desktop - Disable hardwired chrome hot key Ctrl? - Super User](https://superuser.com/a/1207752/95798)
