0

The below code works fine, but if I click double Ctrl+u then it opens all. How can I disable all?

Ctrl+u, Ctrl+s, right-click, F12 key and more key for hide code?

document.onkeydown = function(e) {
  if (e.ctrlKey && (e.keyCode === 67 || e.keyCode === 86 || e.keyCode === 85 || e.keyCode === 117)) { //Alt+c, Alt+v will also be disabled sadly.
    alert('not allowed');
  }
  return false;
};
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
  • May you edit you stacksnippet? The script tags are not needed. – evolutionxbox May 12 '17 at 10:54
  • Even if you block keys and context menus, There are many other ways to reach your code. For Ex: in chrome, options->more tools->developer tools. If you want to hide your JS code, i suggest you obfuscate javascript. https://javascriptobfuscator.com/ – Sankar May 12 '17 at 11:00

2 Answers2

0

you have to put e.stopImmediatePropagation();

/*function check(e)
    {
    alert(e.keyCode);
    }*/
document.onkeydown = function(e) {
  if (e.ctrlKey && (e.keyCode === 67 || e.keyCode === 86 || e.keyCode === 85 || e.keyCode === 117)) { //Alt+c, Alt+v will also be disabled sadly.
    alert('not allowed');
    e.stopImmediatePropagation();
  }
  return false;
};
Edwin
  • 2,146
  • 20
  • 26
0

Try to use the e.preventDefault() function. it will stop the browser to do the default actions when in this case a key combination has been pressed.

The key code for the F12 button is 123. To detect the 'contextmenu' event (user clicks right button), you also have to use the preventdefault function to avoid opening the contextmenu. Maybe this will help you:

Live preview: https://jsfiddle.net/cmLf34h3/1/

document.onkeydown = function(e) {
  if (e.ctrlKey && (e.keyCode === 67 || e.keyCode === 86 || e.keyCode === 85 || e.keyCode === 117) || e.keyCode === 123) { //Alt+c, Alt+v will also be disabled sadly.
    alert('not allowed');
    e.preventDefault();
  }
  return false;
};

window.oncontextmenu = function (e)
{
    alert("You have tested if your right-mousebutton is still working. This alert confirms it's still working, have a nice day!")
    e.preventDefault();
    return false;     // cancel default menu
}

Source for the right-click function: Is right click a Javascript event?

Note: You cannot 100% prevent these actions, there is always a backdoor to bypass this.

I hope this helps

Community
  • 1
  • 1
node_modules
  • 4,790
  • 6
  • 21
  • 37