【js】Mouse prohibits the right button to prohibit opening the console and keyboard prohibition


foreword

Hello everyone, today I will share with you that in the front-end js, it is forbidden to right-click the mouse in the browser, to prohibit the mouse to select and to prohibit keyboard keys.
This method is what I used when making a word assessment system, because if you open the console during the exam and find the correct answer through the console, then it is meaningless.
Therefore, the console needs to be disabled, and some basic knowledge is applied, so let's take a look together!

1. Disable

1. Disable the right mouse button

document.oncontextmenu = new Function("event.returnValue=false");

oncontextmenu is an event attribute in JavaScript, which is used to trigger the corresponding event handler when the user right-clicks the mouse. When the user right-clicks the mouse, the browser will display a context menu. This property can be used to prevent the default context menu behavior, or to customize the behavior of the right-click menu.

2. Disable mouse selection

document.onselectstart = new Function("event.returnValue=false");

onselectstart is an event handler attribute that fires when the user starts selecting text. It can be used to prevent the user from selecting text or to select text while dragging an element.

3. Disable keyboard F12

 document.addEventListener("keydown", function (e) {
    
    
 		console.log(e。key);
        if (e.key == "F12") {
    
    
          e.preventDefault(); // 如果按下键F12,阻止事件
        }
    });

In addition to the above writing, there are other ways of writing.

document.onkeydown = () => {
    
    
  console.log(window.event.keyCode);
  for (let i = 9; i < 47; i++) {
    
    
    if (window.event && window.event.keyCode == i) {
    
    
      return false;
    }
  }
  for (let i = 58; i < 65; i++) {
    
    
    if (window.event && window.event.keyCode == i) {
    
    
      return false;
    }
  }
  for (let i = 91; i < 300; i++) {
    
    
    if (window.event && window.event.keyCode == i) {
    
    
      return false;
    }
  }
  // 禁用ctrl+shift+i
  if (window.event.ctrlKey && window.event.shiftKey && window.event.keyCode == 73) {
    
    
    return false;
  }
  if (window.event.shiftKey) {
    
    
    return false;
  }
  // 禁用ctrl+r
  if (window.event.ctrlKey && window.event.keyCode == 82) {
    
    
    return false;
  }
}

onkeydown is an event handler in JavaScript that fires when the user presses any key on the keyboard. You can use the onkeydown event with a JavaScript function to perform some action when the user presses a key. For example, you can use the onkeydown event to capture the user pressing the Enter key and submitting the form.

You can disable buttons according to your own needs. This is my project need, so I added to disable other keyboard buttons. The ctrl+shift+i above can also open the browser.

2. All codes

// 1.禁用右键菜单
document.oncontextmenu = new Function("event.returnValue=false");
// 2.禁用鼠标选中
document.onselectstart = new Function("event.returnValue=false");

document.onkeydown = () => {
    
    
  console.log(window.event.keyCode);

  for (let i = 9; i < 47; i++) {
    
    
    if (window.event && window.event.keyCode == i) {
    
    
      return false;
    }
  }
  for (let i = 58; i < 65; i++) {
    
    
    if (window.event && window.event.keyCode == i) {
    
    
      return false;
    }
  }
  for (let i = 91; i < 300; i++) {
    
    
    if (window.event && window.event.keyCode == i) {
    
    
      return false;
    }
  }
  // 禁用ctrl+shift+i
  if (window.event.ctrlKey && window.event.shiftKey && window.event.keyCode == 73) {
    
    
    return false;
  }
  if (window.event.shiftKey) {
    
    
    return false;
  }
  // 禁用ctrl+r
  if (window.event.ctrlKey && window.event.keyCode == 82) {
    
    
    return false;
  }
}

Summarize

Finally, I will secretly tell you that there is another way to open the console, which is to press F12 while refreshing the browser page, which can also be opened, so I also did some operations on the refresh in this project.
You can try this method of opening the console, but you must disable F12 first, otherwise you will not know if you make a mistake. This method is not particularly easy to open, and you need to try a few more times.
The above is the whole content of this chapter, thank you for reading, hope to help you.

Guess you like

Origin blog.csdn.net/SqlloveSyn/article/details/131264001