I received a new requirement in a project that we have to prevent Windows' alt+tab hot keys to prevent switches between windows.
After a lot of effort, I can prevent alt,ctrl,tab,shift,ctrl+s,ctrl+c,ctrl+v, etc., but I'm not able to prevent alt+tab, whether it's Firefox or Chrome.
I searched in MDN and finally found this: prevent the default action of the corresponding key down event in Chrome
So my guess is that alt+tab is a Windows system hot key but not the browser's. And event.preventDefault()
can only prevent the events corresponding to the browser.
Does anyone have a more detailed explanation?
The following is my code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script>
document.addEventListener("keydown", function(e) {
//tab keyCode===9
//I hope to prevent alt+tab event action in windows
if (e.altKey && e.keyCode === 9) {
e.preventDefault(); //why not e in?
}
}, false)
</script>
</body>
</html>
I received a new requirement in a project that we have to prevent Windows' alt+tab hot keys to prevent switches between windows.
After a lot of effort, I can prevent alt,ctrl,tab,shift,ctrl+s,ctrl+c,ctrl+v, etc., but I'm not able to prevent alt+tab, whether it's Firefox or Chrome.
I searched in MDN and finally found this: prevent the default action of the corresponding key down event in Chrome
So my guess is that alt+tab is a Windows system hot key but not the browser's. And event.preventDefault()
can only prevent the events corresponding to the browser.
Does anyone have a more detailed explanation?
The following is my code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script>
document.addEventListener("keydown", function(e) {
//tab keyCode===9
//I hope to prevent alt+tab event action in windows
if (e.altKey && e.keyCode === 9) {
e.preventDefault(); //why not e in?
}
}, false)
</script>
</body>
</html>
Share
Improve this question
edited Nov 5, 2016 at 2:47
Makyen♦
33.3k12 gold badges92 silver badges125 bronze badges
asked Nov 5, 2016 at 2:19
Ye ShiqingYe Shiqing
1,6592 gold badges17 silver badges24 bronze badges
7
- Probably similar to CRTL+S in JSFiddle displaying a popup in firefox about saving the page. – J. Allan Commented Nov 5, 2016 at 2:22
- @JefréN. I can prevent CTRL+S now but not the alt+tab – Ye Shiqing Commented Nov 5, 2016 at 2:25
- 3 Firstly I'd question your usability impact here, given that it's an Operating System level shortcut, there will be an expectation by the user that it will continue to work. As such alt+tab is considered by the OS before it is passed to the browser so you won't see the keydown for "tab" if "alt" is also held down. In short, question the requirement, and don't attempt mess with OS level shortcuts ;). – JVDL Commented Nov 5, 2016 at 2:35
- 3 I don't see how someone would even begin to think that a webpage running in a browser would be able to affect OS/system level hotkeys (e.g. prevent them from working). If that was possible, all sorts of malicious websites would do so all the time. – Makyen ♦ Commented Nov 5, 2016 at 2:35
- 1 If you are setting up a kiosk, then you can explore system level changes that allow you to prevent user's from breaking out of the application (i.e. the browser) and reduce the things a user can do from a particular account. There are also browser add-ons which restrict what users can do within the browser. There are valid use cases for preventing the use of Alt-Tab, just not for a webpage intended to be viewed normally on the web. – Makyen ♦ Commented Nov 5, 2016 at 2:54
1 Answer
Reset to default 20
event.preventDefault()
can only prevent the events corresponding to the browser.
More precisely, event.preventDefault()
can only prevent the action of events that the browser receives and decides to pass on to the web page.
Alt+Tab is handled outside the browser entirely, by the Windows DWM. Under most circumstances, this shortcut isn't passed to desktop applications at all. Since web browsers don't go out of their way to capture it, they will never "see" the event at all. The same principle applies for Ctrl+Alt+Del.
Some browsers will also "protect" certain keyboard shortcuts by never passing them to the web page, so that users can rely on these shortcuts always behaving consistently. For example, Chrome will protect any keyboard event which maps to one of the following mands:
- Close Tab (Ctrl+W)
- Close Window (Ctrl+Shift+W)
- New Incognito Window (Ctrl+Shift+N)
- New Tab (Ctrl+T)
- New Window (Ctrl+N)
- Restore Tab (Ctrl+Shift+T)
- Select Next Tab (Ctrl+Tab, Ctrl+PageDown)
- Select Previous Tab (Ctrl+Shift+Tab, Ctrl+PageUp)
- Exit (Alt+F4, Cmd+Q)