2

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 combination 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 accomplish the above?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Nathan
  • 6,772
  • 11
  • 38
  • 55
  • Do you really think that this is possible? Disabling keys on the client computers keyboard from a public web page over the internet? I don't think so :-) – Darin Dimitrov Jun 19 '11 at 16:41
  • @Darin I hope it is ;). But, honestly, I don't know. That's why I asked here. – Nathan Jun 19 '11 at 16:46

3 Answers3

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 computer 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.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • Oh, I know. This is just an example. Ignore the screenshot part. I know full well I can't prevent those. – Nathan Jun 19 '11 at 16:43
  • @Nathan ah, okay. Content protection is a bad way to start a SO question ;) you can *try* catching the combinations using the [jQuery shortcuts plugin](http://code.google.com/p/js-hotkeys/) for example. I'm not sure to what extent browsers allow overriding their own combinations though. – Pekka Jun 19 '11 at 16:45
  • I see that the plugin you link to lets me repurpose browser hotkeys. That's as close as I'm going to get, it looks like. Thanks! – Nathan Jun 19 '11 at 16:51
  • In 2011, not being able to block screenshotting of content was true, but today it *is* possible due to the evolution of DRM in browsers. For example, try taking a screenshot of Netflix with a show playing. *How* exactly one can use the Encrypted Media Extensions themselves is a bit unclear though. But here is a little more info on how Netflix does it: https://stackoverflow.com/q/63175756/149428. – Taylor D. Edmiston Jun 08 '23 at 16:46
1

You cannot block keyboard combinations that belong to the OS. Only keyboard combinations 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

Caspar Kleijne
  • 21,552
  • 13
  • 72
  • 102
1
// 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)

gblazex
  • 49,155
  • 12
  • 98
  • 91