Skip to content Skip to sidebar Skip to footer

Can I Enable/disable Client Num-lock Using Javascript?

I'm developing c# web application and I want to let user use num-lock as a short key for switch some options on my app but don't want it to disable the num-pad. Can I achieve this

Solution 1:

Well, this answer performs a check for which Key is pressed and accrodingly processes your required operation!

Main part in JS code is :

var keycode = (event.keyCode ? event.keyCode : event.which);
$('#keydownCode').html(keycode);

And here is the Demo for it!

Working Demo

Edit :

After explanation for "Disabling" NUM lock; i.e. reject keystrokes if NUM LOCK is OFF, I've created below quick piece of code!

It doesn't accept values when NUM LOCK is OFF!

Another Demo

Solution 2:

Here is the closest to what I'm looking for jsFiddle

 $('#textbox').keydown(function(event){
    var keycode = (event.keyCode ? event.keyCode : event.which);
    $('#keydownCode').html(keycode);


 if(keycode == 144){
    $('#Typoef').html('It is a Sell');
     //perform Sell operation here
 }
 elseif(keycode == 109){
    $('#Typoef').html('It is a Purchase');
     //perform Purchase operation here
 }
 elseif(keycode == 35){
    $('#textbox').val($('#textbox').val()+"1");
     e.preventDefault();
 }
 elseif(keycode == 40){
    $('#textbox').val($('#textbox').val()+"2");
     e.preventDefault();
 }
 elseif(keycode == 34){
    $('#textbox').val($('#textbox').val()+"3");
     e.preventDefault();     
 }
 elseif(keycode == 37){
     $('#textbox').val($('#textbox').val()+"4");
     e.preventDefault(); 
 }
 elseif(keycode == 12){
    $('#textbox').val($('#textbox').val()+"5");
     e.preventDefault();     
 }
 elseif(keycode == 39){
    $('#textbox').val($('#textbox').val()+"6");
     e.preventDefault();     
 }
 elseif(keycode == 36){
    $('#textbox').val($('#textbox').val()+"7");
     e.preventDefault();     
 }
 elseif(keycode == 38){
    $('#textbox').val($('#textbox').val()+"8");
     e.preventDefault();     
 }elseif(keycode == 33){
    $('#textbox').val($('#textbox').val()+"9");
     e.preventDefault();     
 }
 elseif(keycode == 45){
    $('#textbox').val($('#textbox').val()+"0");
     e.preventDefault();     
 }
 else{
     $('#Typoef').html('');
 }
});

Solution 3:

Well - it appears that the NumLock key (am quite surprised by this) uses keycode 144 on keydown - so you can pick it up and use it as a "hot key".

See this jsFiddle

$(function(){
    $("input").keydown(function(e){
        alert(e.which);
    });
});

If you want it as a "use it anywhere hot key" you'll want to bind your keydown handler to the body or document rather than a specific element.

Further comment re: disabling the use of numlock (but still wanting to use the num pad). a) you can't - it's a hardware thing, and b) you've got to question why you would ever want to do that.... if you could, what happens when the user goes away from the browser (and toggles the numlock without you knowing it) - suggest a re-think. (sorry)

Post a Comment for "Can I Enable/disable Client Num-lock Using Javascript?"