Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery disabling a keyboard key

Tags:

html

jquery

Is it possible to disable a certain keyboard key (like asterisk or string) by using jquery?

like image 667
Lvcky Mercado Avatar asked Jan 10 '12 14:01

Lvcky Mercado


3 Answers

You can't disable a key stroke as such but you could capture it with jQuery and overwrite its action (return false). The example below captures the enter key. Just change 13 to any key code you need to disable.

$("input").keypress(function (evt) {

  var keycode = evt.charCode || evt.keyCode;
  if (keycode  == 13) { //Enter key's keycode
    return false;
  }
});
like image 88
detheridge02 Avatar answered Nov 12 '22 02:11

detheridge02


I had to disable all the keys but the F11 and ESC. So I did this way. Think this may help someone. :)

$(document).on('keydown',function(e)
{ 
    var key = e.charCode || e.keyCode;
    if(key == 122 || key == 27 )
        {}
    else
        e.preventDefault();
});
like image 34
Ijas Ameenudeen Avatar answered Nov 12 '22 04:11

Ijas Ameenudeen


Handle onKeyDown, then preventDefault on it.

like image 1
Amadan Avatar answered Nov 12 '22 03:11

Amadan