1

I want to catch the event with javascript or jquery when user presses to the delete button in keyboard. Is this possible?

Afsun Khammadli
  • 2,048
  • 4
  • 18
  • 28

2 Answers2

4

I'm not sure why you're being told that this is impossible?

The delete key is 46, so this should work:

$('html').keyup(function(e){
    if(e.keyCode == 46){
        //delete key has been pressed.
    }
})

Also note: You should be using either the keyup or keydown event rather than keypress. keypress is intended for real character. 'keyup'/'keydown' is handled at a lower level so it will capture all non-printing keys like DEL and ENTER.

Source is here: Capturing "Delete" Keypress with jQuery

Community
  • 1
  • 1
johnkavanagh
  • 4,614
  • 2
  • 25
  • 37
  • You're missing a `;`. Use `which` (it's shorter!) (Kidding) – Roko C. Buljan May 29 '13 at 11:48
  • 'Semicolons in JavaScript are optional': http://mislav.uniqpath.com/2010/05/semicolons/ Take a read! – johnkavanagh May 29 '13 at 11:49
  • Thanks @RokoC.Buljan I'm aware of the limitations with minification. In this instance it would make no difference. For reference: many JS compilers/minifiers will strip unnecessary semi-colons, including the YUI Compressor. – johnkavanagh May 29 '13 at 11:52
  • Glad to hear it @ƏfsunXəmmədli - feel free to open up more questions in the future if you need further guidance from people here. If your question has been answered, do remember to put a green tick against the relevant answer so that anyone coming to the same problem in the future can find the answer as easily as possible. – johnkavanagh May 30 '13 at 08:24
0
$('html').keyup(function(e){
    if(e.keyCode == 46)alert('Delete Key Pressed')
})

from here

Community
  • 1
  • 1
Bhavin Rana
  • 1,554
  • 4
  • 20
  • 40