如何使用 JavaScript 在 Microsoft Edge (Ctrl + Shift + T) 中禁用 "reopen last tab"?
How to disable "reopen last tab" in Microsoft Edge (Ctrl + Shift + T) using JavaScript?
我们有一个定义了一堆键盘快捷键的网络应用程序。其中之一是 Ctrl + Shift + T。问题是 Microsoft Edge 连接到此组合键以重新打开之前关闭的选项卡或 window,然后切换到它。
参考:Keyboard Shortcuts in Microsoft Edge.
我玩了一下,试图用 JavaScript:
来规避这个问题
<!DOCTYPE html>
<html>
<head>
<title>Ctrl + Shift + T</title>
<script type="text/javascript">
var tKey = 84;
function logKeys(event) {
console.log(event.type + ": " + event.keyCode);
if (event.keyCode === tKey) {
event.preventDefault();
event.stopPropagation();
setTimeout(function() { window.focus(); }, 200);
}
}
document.documentElement.addEventListener("keydown", logKeys);
document.documentElement.addEventListener("keypress", logKeys);
document.documentElement.addEventListener("keyup", logKeys);
</script>
</head>
<body>
</body>
</html>
我希望使用 JavaScript“取消”键盘事件,这样 Edge 就不会重新打开之前打开的选项卡或 window。 Edge 键盘快捷键仍然优先。事实上,浏览器控制台甚至没有为 T 键注册日志消息,如 event.keyCode
84.
所示
也许我的 JavaScript 排骨有点生锈了。
有没有办法阻止 Edge 在按下 Ctrl + Shift[= 时重新打开上一个选项卡或 window 35=] + T 使用 JavaScript?
这可以使用(目前处于试验阶段)Keyboard Lock API,它在 Chrome 和 Edge 中可用(但目前在 IE、Firefox 或 Safari 中不可用):
Richly interactive web sites, games and remote desktop/application streaming experiences want to provide an immersive, full screen experience. To accomplish this, sites need access to special keys and keyboard shortcuts while they are in full screen mode so that they can be used for navigation, menus or gaming functionality. Some examples of the keys that may be required are Escape, Alt+Tab, Cmd+`, and Ctrl+N.
By default, these keys are not available to the web application because they are captured by the browser or the underlying operating system. The Keyboard Lock API enables websites to capture and use all available keys allowed by the OS.
请注意,出于可用性原因,此 API 仅在您的应用程序使用全屏 API 启动全屏模式 时才有效。否则您不能干扰标准浏览器键盘快捷键:
There are two different types of fullscreen available in modern user agents: JavaScript-initiated fullscreen (via the [Fullscreen] API) and user-initiated fullscreen (when the user enters fullscreen using a keyboard shortcut). The user-initiated fullscreen is often referred to as "F11" fullscreen since that is a common key shortcut used to enter and exit fullscreen mode.
F11 fullscreen and JavaScript (JS) fullscreen do not behave the same way. When a user enters F11 fullscreen, they can only exit it via the same keyboard shortcut that they used to enter it -- the exitFullscreen() function will not work in this case. In addition, fullscreen events that are normally fired for JS fullscreen are not sent for F11 fullscreen.
Because of these differences (and because there is no standard shortcut for F11 fullscreen), the Keyboard Lock API is only valid when the a JavaScript-initiated fullscreen is active. During F11 fullscreen, no Keyboard Lock processing of keyboard events will take place.
所以在你的情况下,要捕获 Ctrl+Shift+T 你需要使用全屏 API 进入全屏模式(您可能希望首先获得用户的同意;意外进入全屏模式将是糟糕的用户体验),然后锁定 T 键使用键盘锁 API:
document.documentElement.requestFullscreen().then(() => {
console.log("In fullscreen.");
navigator.keyboard.lock(["KeyT"]).then(() => {
console.log("Ctrl+Shift+T locked");
});
})
更多信息:
我们有一个定义了一堆键盘快捷键的网络应用程序。其中之一是 Ctrl + Shift + T。问题是 Microsoft Edge 连接到此组合键以重新打开之前关闭的选项卡或 window,然后切换到它。
参考:Keyboard Shortcuts in Microsoft Edge.
我玩了一下,试图用 JavaScript:
来规避这个问题<!DOCTYPE html>
<html>
<head>
<title>Ctrl + Shift + T</title>
<script type="text/javascript">
var tKey = 84;
function logKeys(event) {
console.log(event.type + ": " + event.keyCode);
if (event.keyCode === tKey) {
event.preventDefault();
event.stopPropagation();
setTimeout(function() { window.focus(); }, 200);
}
}
document.documentElement.addEventListener("keydown", logKeys);
document.documentElement.addEventListener("keypress", logKeys);
document.documentElement.addEventListener("keyup", logKeys);
</script>
</head>
<body>
</body>
</html>
我希望使用 JavaScript“取消”键盘事件,这样 Edge 就不会重新打开之前打开的选项卡或 window。 Edge 键盘快捷键仍然优先。事实上,浏览器控制台甚至没有为 T 键注册日志消息,如 event.keyCode
84.
也许我的 JavaScript 排骨有点生锈了。
有没有办法阻止 Edge 在按下 Ctrl + Shift[= 时重新打开上一个选项卡或 window 35=] + T 使用 JavaScript?
这可以使用(目前处于试验阶段)Keyboard Lock API,它在 Chrome 和 Edge 中可用(但目前在 IE、Firefox 或 Safari 中不可用):
Richly interactive web sites, games and remote desktop/application streaming experiences want to provide an immersive, full screen experience. To accomplish this, sites need access to special keys and keyboard shortcuts while they are in full screen mode so that they can be used for navigation, menus or gaming functionality. Some examples of the keys that may be required are Escape, Alt+Tab, Cmd+`, and Ctrl+N.
By default, these keys are not available to the web application because they are captured by the browser or the underlying operating system. The Keyboard Lock API enables websites to capture and use all available keys allowed by the OS.
请注意,出于可用性原因,此 API 仅在您的应用程序使用全屏 API 启动全屏模式 时才有效。否则您不能干扰标准浏览器键盘快捷键:
There are two different types of fullscreen available in modern user agents: JavaScript-initiated fullscreen (via the [Fullscreen] API) and user-initiated fullscreen (when the user enters fullscreen using a keyboard shortcut). The user-initiated fullscreen is often referred to as "F11" fullscreen since that is a common key shortcut used to enter and exit fullscreen mode.
F11 fullscreen and JavaScript (JS) fullscreen do not behave the same way. When a user enters F11 fullscreen, they can only exit it via the same keyboard shortcut that they used to enter it -- the exitFullscreen() function will not work in this case. In addition, fullscreen events that are normally fired for JS fullscreen are not sent for F11 fullscreen.
Because of these differences (and because there is no standard shortcut for F11 fullscreen), the Keyboard Lock API is only valid when the a JavaScript-initiated fullscreen is active. During F11 fullscreen, no Keyboard Lock processing of keyboard events will take place.
所以在你的情况下,要捕获 Ctrl+Shift+T 你需要使用全屏 API 进入全屏模式(您可能希望首先获得用户的同意;意外进入全屏模式将是糟糕的用户体验),然后锁定 T 键使用键盘锁 API:
document.documentElement.requestFullscreen().then(() => {
console.log("In fullscreen.");
navigator.keyboard.lock(["KeyT"]).then(() => {
console.log("Ctrl+Shift+T locked");
});
})
更多信息: