Tuesday 27 July, 2010

Javascript to disable a particular key

Use this function to disable certain keys. This essentially tells the page to cancel the last event, e.g. the pressing of that certain key.

In this demo, the CAPS LOCK key is disabled. Try to press it.

document.onkeydown = checkKeycode

function checkKeycode(e)
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
if(keycode == 20)
{
void(0);
alert("You are not allowed to use the CAP LOCKS key!");
}
}

No comments:

Post a Comment