Blocking Any Event Using JavaScript Codes

Introduction

 
In most browsers there are three possible kinds of events triggered when a keyboard key is pressed or released,
  1. keydown
  2. keypress
  3. keyup
You can check which key was pressed or released whenever KeyboardEvent is triggered because that event contains information and based on that event information you can write you custom logic to handle that event.
 
Note
 
The implementation will fail if the user disables JavaScript.
 
Base Code to trace any javascript event,
  1. document.addEventListener("keydown"function(event)  
  2. {  
  3.    console.log(event.which);  
  4.    console.log(event.keyCode);  
  5.    console.log(event.shiftKey);  
  6.    console.log(event.altKey);  
  7.    console.log(event.ctrlKey);  
  8.    console.log(event.metaKey);  
  9. }   
Live Example - https://codepen.io/varunjoshi/pen/RmpxJR
 
List of Available Javascript Code ,
 
Blocking Any Event Using JavaScript Codes
 
We can use the below code to prevent the opening of the context menu, copy cut paste, or view source code on any page.
 
Many times we want to disable some existing functionality on a webpage for any security reason.
 
Eg: prevent images from being downloaded, copy the content, etc...,
 
Disable context menu on right-click,
  1. $("body").on("contextmenu"function (e)  
  2.    {  
  3.       return false;  
  4.    });  
  5. }); 
Or,
  1. document.oncontextmenu = function() {
  2.    return false;
  3. }
Disable right-click menu on a particular section on the page,
  1. $(document).ready(function(){  
  2.    $("#youDivSectionId").bind("contextmenu"function(e) {  
  3.       return false;  
  4.    });  
  5. }); 
Disable Cut, copy, paste,
  1. $(document).ready(function(){
  2.    $('body').bind('cut copy paste', function (e)
  3.    {
  4.       e.preventDefault();
  5.    });
  6. });
Let's Block the same cut, copy, paste events with javascript codes,
  1. $(document).ready(function(){  
  2. $(document).keydown(function(event) {  
  3.    //event.ctrlKey = check ctrl key is press or not  
  4.    //event.which = check for F7  
  5.    // event.which =check for v key  
  6.    if (event.ctrlKey==true && (event.which == '118' || event.which == '86')) {  
  7.       event.preventDefault();  
  8.       }  
  9.    });  
  10. }); 
Prevent browser Debugger console example,
  1. $(document).keydown(function (event) {
  2. // Prevent F12 -
  3. if (event.keyCode == 123) {
  4.    return false;
  5. }
  6. // Prevent Ctrl+a = disable select all
  7. // Prevent Ctrl+u = disable view page source
  8. // Prevent Ctrl+s = disable save
  9. if (event.ctrlKey && (event.keyCode === 85 || event.keyCode === 83 || event.keyCode ===65 )) {
  10.    return false;
  11. }
  12. // Prevent Ctrl+Shift+I = disabled debugger console using keys open
  13. else if (event.ctrlKey && event.shiftKey && event.keyCode === 73)
  14. {
  15.    return false;
  16. }
  17. });
Using Javascript code you can block any event of browser.