admin管理员组文章数量:1399509
I want to detect and block certain keyboard shortcuts on a web page. For example, say I want to prevent alt+tab for switching apps (just an example, assume any global shortcut).
Here's as far as I can think it out:
- attach a keyboard event listener to
document
(orwindow
?) - use
event.which
to check which key bination was pressed - if it was a blacklisted shortcut, stop the browser from executing it
But, I don't know how to
A) detect multiple keys (e.g. alt and tab together), or
B) stop them from executing (can I just return false
?).
Can anyone tell me how to acplish the above?
I want to detect and block certain keyboard shortcuts on a web page. For example, say I want to prevent alt+tab for switching apps (just an example, assume any global shortcut).
Here's as far as I can think it out:
- attach a keyboard event listener to
document
(orwindow
?) - use
event.which
to check which key bination was pressed - if it was a blacklisted shortcut, stop the browser from executing it
But, I don't know how to
A) detect multiple keys (e.g. alt and tab together), or
B) stop them from executing (can I just return false
?).
Can anyone tell me how to acplish the above?
Share Improve this question edited Aug 16, 2021 at 8:53 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Jun 19, 2011 at 16:39 NathanNathan 7,04212 gold badges40 silver badges56 bronze badges 2- Do you really think that this is possible? Disabling keys on the client puters keyboard from a public web page over the internet? I don't think so :-) – Darin Dimitrov Commented Jun 19, 2011 at 16:41
- @Darin I hope it is ;). But, honestly, I don't know. That's why I asked here. – Nathan Commented Jun 19, 2011 at 16:46
3 Answers
Reset to default 6You want to prevent screenshots from being taken? Forget it right now.
No matter what elaborate mechanisms you put into place, they will be trivial to circumvent by un-focusing the browser window (or just the document, e.g. by clicking into the address bar), and pressing the screenshot key then.
There is no chance for you to do this except by installing client software on the puter that controls what the user does, or maybe using some proprietary ActiveX control that makes its contents un-print-screenable. Both approaches are hugely difficult and have tons of downsides.
You cannot block keyboard binations that belong to the OS. Only keyboard binations that roam inside the browser and are not OS specific.
If you want to protect your content, don't publish it in public. Or put a decent license on it
// lookup table for keycodes
var key = { s: 83 };
document.onkeydown = function (e) {
// normalize event
e = e || window.event;
// detecting multiple keys, e.g: Ctrl + S
if (e.ctrlKey && !e.altKey && e.keyCode === key.s) {
// prevent default action
if (e.preventDefault) {
e.preventDefault();
}
// IE
e.returnValue = false;
}
};
Detecting Keystrokes Compatibility Table (QuirksMode)
本文标签: javascriptWeb page detect and block certain keyboard shortcutsStack Overflow
版权声明:本文标题:javascript - Web page: detect and block certain keyboard shortcuts - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744194101a2594654.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论