Play with JavaScript: Capturing Control Keys

This is the practical scenario where sometime one wants to disallow Ctl+keys combinitions on web-pages.

Following Code-snippet tells the whole story
  1. < html >  
  2. < head >  
  3. < script language = "JavaScript" > function testCtrlKeys(e)  
  4. {  
  5.     //Create an array for all possible keys   
  6.     var arrCtrlKeys = new Array('a''n''c''x''v''j');  
  7.     var key;  
  8.     var isCtrl;  
  9.   
  10.     if (window.event)   
  11.     {  
  12.         key = window.event.keyCode; //Valid only for Internet Explorer   
  13.         if (window.event.ctrlKey) isCtrl = true;  
  14.         else isCtrl = false;  
  15.     } else   
  16.     {  
  17.         key = e.which; //this is other than IE   
  18.         if (e.ctrlKey) isCtrl = true;  
  19.         else isCtrl = false;  
  20.     }  
  21.   
  22.   
  23.     //if ctrl is pressed check if other key is in forbidenKeys array   
  24.     if (isCtrl)  
  25.     {  
  26.         for (i = 0; i < arrCtrlKeys.length; i++)  
  27.         {  
  28.             //case-insensitive comparation   
  29.             if (arrCtrlKeys[i].toLowerCase() == String.fromCharCode(key).toLowerCase())  
  30.             {  
  31.                 alert('You have pressed CTRL + ' + String.fromCharCode(key) + '.');  
  32.   
  33.                 return false;  
  34.             }  
  35.         }  
  36.     }  
  37.     return true;  
  38. } < /script>   
  39.   
  40. </head >  
  41. < body >   
  42. < form method = "" >  
  43. < div > < h3 > Test Key Combinition[Ctrl + A, Ctrl + N, Ctrl + C, Ctrl + X, Ctrl + V, Ctrl + J] < /h3>   
  44. <input type="text" name="mytext"   
  45. onKeyPress="return testCtrlKeys(event);"   
  46. onKeyDown="return testCtrlKeys(event);" / >  
  47. < /div>   
  48. </body >  
  49. < /html>  
Please note that

Copy/paste above code in text file and save it as "capturectrlkeys.html" and run it into Explorer
Check the magic of capturing javascript keys.