javascript - Web page: detect and block certain keyboard shortcuts - Stack Overflow

I want to detect and block certain keyboard shortcuts on a web page. For example, say I want to prevent

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 (or window?)
  • 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 (or window?)
  • 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
Add a ment  | 

3 Answers 3

Reset to default 6

You 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)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744194101a4562570.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信