Disable keyboard keys using javascript or jquery

by  
Share:
Disable keyboard keys using javascript or jquery

Javascript/Jquery snippet to disable keyboard keys on a webpage. You can also allow a particular key like enter key and disable the rest

Disable keyboard keys using jquery

<script type='text/javascript'>
  $(document).keydown(function(e){
    e.preventDefault();
  });
</script>

Allowing certain keyboard buttons and disabling the rest. In this case allowing the windows key using jquery.
<script type='text/javascript'>
$(document).keydown(function(e){
  var key = e.charCode || e.keyCode;
  if (key == 13) { 
    // enter key do nothing
  } else {
    e.preventDefault();
  }	
});
</script>

Disable keyboard keys using javascript

<script type='text/javascript'>
  document.onkeydown = function (e) {
    e.preventDefault();		
  }
</script>

Allowing certain keyboard buttons and disabling the rest. In this case allowing the windows key using javascript.
<script type='text/javascript'>
document.onkeydown = function (e) {
  var key = e.charCode || e.keyCode;
  if (key == 13) { 
    // enter key do nothing
  } else {
    e.preventDefault();
  }	     
}
</script>

Part of the code has been taken from this post. Key codes list can be found here.

Get the latest updates directly in your inbox for FREE: Subscribe here

Share your feedback / let me know your doubts regarding this tutorial in the comment box given below.

Share:
Hasan

Shahrukh Khan (Hasan)

A software engineer who's a die-hard coder, blogger, dreamer and mentor with years of expertise in web development. Know more...

Related Posts

Tarun on

Can you disable alt ctrl dlt key through java script.

Reply
eyphah on

//To disable a particular key:

window.addEventListener(“keydown”, function (e) {

if ([key code1,key code2 …].indexOf(e.keyCode) > -1) {
e.preventDefault();
}
}, false);

Reply
priya on

Can you disable escape key through java script.

Reply
Shahrukh Khan on

yes use 27 code

Reply
Rashmi on

Can You Help Me pls…..
i want disable only ctrl key using java script.

Reply
Rashmi on

I find code 17
but where i put this actually pls help

Shahrukh Khan on

see the script in the tutorial remove the number with 17

wita on

How to turn off /disable the keyboard in chrome??its work at mozila fire fork,,,,

Reply
RAmp on

Hi, I need to prevent ‘Ctrl+a’ in the input element.
How to do that? help me.

Reply
RAmp on

I got it..!

Reply
RAmp on

Actually, I need to prevent the zero as the first letter in my input element.
I have normally prevented on click or pressing the key by:
(e.keyCode == 96 || e.keyCode == 48) – prevent default.
The problem is,
While input has some numbers, that can be selected and after that, if click the zero, it’s not able to prevent it..!
can you give me some idea to prevent selection and ctrl+a in the input element? unless the zero can be prevented I think.

Shahrukh Khan on

why do you need to do this?

Shahrukh Khan on

just disable ctrl key

Reply
kanu on

can u have disable window key and print screen at a time

Reply
Shahrukh Khan on

yes all you need is to know the code of the key.

Reply
avneet on

this code not working

Reply
avneet on

how disable esc key on safari

Reply
Kannan on

Hi,
i have one problem to disable shortcut keys in chrome and firefox browsers,…

browser event ctrl+n – i need disable and execute my function in my web page.

Thanks in advance
Kannan – Developer

Reply
Shahrukh Khan on

check the ctrl key event code. its available online.

Reply
Anthony on

how do you disable multiple keys at once in one script? thank you

Reply
Shahrukh Khan on

thats a good question. will have to do some research. in case if you find the solution do post it here.

Reply
santosh on

escape and windows key is not working when fullscreen mode is on.
is there any solution for this scenario

Reply
Shahrukh Khan on

its a tricky question. will have to find out. meanwhile if you find the answer do post it here.

Reply
Bharath on

HI bro,

How can disable the key after second press same key.

#(document).on (‘keydown’ , function (e){
var userVal = e.which ∥ e.keycode
valadation (userVal)

})

function valadation (userVal){
if (37 == userVal){
alert (“Wecome”)
}
}

I have written my code above, when press first time 37 it showing alert message, again press the same key second time I want to disable that key. How can do that?

Reply
Shahrukh Khan on

i didnt get you about the second key.

Reply
Mujahid Bhoraniya on

How do i disable Right Click in my website. Please help me

Reply
dinesh on

Is disabling every keyevent by preventdefault or returning false is the only way…??? if not what are the other ways…

Reply
Shahrukh Khan on

you can use any of those

Reply
ram on

How to Disble print screen in Website using javascript?

Reply
Shahrukh Khan on

well JavaScript can always be disabled through the browser, so I think its an unnecessary thing to do.

Reply
vinit on

can u disable metakey code is 91

Reply
David Max on

Really awesome article. Enjoyed while reading, also it helped to understand how useful it is. Keep sharing!

Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.