views:

291

answers:

3

I have a function that is triggered by the onkeydown event of a textbox. I need to know if the user has hit either the backspace key or the del key.

Thanks in advance

+3  A: 

In your function check for the keycode 8 (backspace) or 46 (delete)

Keycode information
Keycode list

jasonbar
+1  A: 

Try this:

document.onkeydown = KeyCheck;  //or however you are calling your method
function KeyCheck()
{
   var KeyID = event.keyCode;
   switch(KeyID)
   {
      case 8:
      alert("backspace");
      break; 
      case 46:
      alert("delete");
      break;
      default:
      break;
   }
}
Stephano
A: 

not sure if it works outside of firefox:

callback (event){
  if (event.keyCode === event.DOM_VK_BACK_SPACE || event.keyCode === event.DOM_VK_DELETE)
    // do something
  }
}

if not, replace event.DOM_VK_BACK_SPACE with 8 and event.DOM_VK_DELETE with 46 or define them as constant (for better readability)

mathroc