donderdag 5 april 2012

Block Excape and Ctrl+z keypress to prevent the undo action by keyboard


One of the disadvantages of using Jscript of JQuery is when a user uses 
the Escape or CTRL+z on his keyboard that 
your actions, like filling in input fields, in your scripting are being undone.

So best option in this case is to prevent that thoose events are being executed.
This you can do by capturing the keypresses of the Escape key and the CTRL+z key combination.
The script below will execute on the document Keydown event. 

 function DisableEscAndCtrlZ(){
  document.onkeydown = function(evt) {
   evt = evt || window.event;
   // ctrl+z
   if (evt.ctrlKey && evt.keyCode == 90) {
    return false;  
   }
   // esc
   if (evt.keyCode == 27) {
    return false;  
   }
  };
 }

 $(document).ready( function() {
  DisableEscAndCtrlZ();
 };
 

Geen opmerkingen:

Een reactie posten