Here is the code that can manifest that thought of yours.
<script> (document.onkeydown = function (e) { e.preventDefault(); } </script>
The code above disables the entire keyboard. Your visitor will not be able to use it at all. Document refers to the webpage that this script is used on. It calls the Javascript event handler "onkeydown". We then use the object of that event handler to call the default behavior. The one below disables all the keys except the ENTER or CARRIAGE RETURN button.
<script> document.onkeydown = function (e) { var key = e.charCode || e.keyCode; if (key == 13) { // enter key do nothing } else { e.preventDefault(); } } </script>
We used the charCode or keyCode to permit the ENTER key on the webpage. The ASCII code for ENTER button is 13. So therefore, it is used in the if conditional statement to allow the key to be used. To help us exclude more keys instead of going through the stress of developing your own on-web keyboard, the image below contains all the ASCII code associated with each keyboard key. Find the code corresponding to the key you want to enable on the keyboard and use it in the if conditional statement like the one above. You can create an array that could hold all the keyCode or charCode and loop through them to get things done. This will save you multiple lines of code. Happy coding!
0 comments:
Post a Comment