Define a decimal (exact) key from the numeric keypad

I need to determine if the user has pressed the dot key on the numeric keypad. I wrote this first draft that works for me:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head><title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript"><!--
(function($){
    var knownCodes = [
        [110, 46], // Firefox, IE, Chrome
        [78, 46] // Opera
    ];
    var pressedCodes = [null, null];

    $.fn.comma = function(){
        this.live("keydown", function(e){
            pressedCodes = [e.which, null];

        }).live("keypress", function(e){
            pressedCodes[1] = e.which;

            for(var i=0, len=knownCodes.length; i<len; i++){
                if(pressedCodes[0]==knownCodes[i][0] && pressedCodes[1]==knownCodes[i][1]){
                    $("#log").append("<li>Decimal key detected</li>");
                    break;
                }
            }
        });

        return this;
    };
    $(function(){
        $('<ol id="log"></ol>').appendTo("body");
    });
})(jQuery);

jQuery(function($){
    $(".comma input:text, .comma textarea").css("border", "1px solid black").comma();
});
//--></script>
</head>
<body>

    <form action="" method="get" class="comma" size="20">
        <p><input type="text"></p>
        <p><textarea rows="3" cols="30"></textarea></p>
    </form>

</body>
</html>

However, I can only test it on Windows XP with a Spanish keyboard. My questions:

  • Is it safe to read e.which? I use it because it e.keyCodeand e.charCodereturn undefinedat least one browser.

  • Does the operating system affect these numeric codes in any way or only the browser material?

  • Do these codes match the keyboard layout?


Background information: I could not find the jQuery plugin for repeated numeric keypad , so I write my own.

Update

. .. ,. -. , MS Excel, , . .

script, , . Codes:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head><title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript"><!--
(function($){
    $.fn.showKeyCodes = function(){
        var log = function(e){
            $("<li></li>").text(e.type + "(): [keyCode, charCode, which]=[" + e.keyCode + ", " + e.charCode + ", " + e.which + "]").appendTo("#log");
        }

        this.live("keydown", function(e){
            log(e);

        }).live("keypress", function(e){
            log(e);

        }).live("keyup", function(e){
            log(e);

        });

        return this;
    };
    $(function(){
        $('<ol id="log"></ol>').appendTo("body");
    });
})(jQuery);

jQuery(function($){
    $(".showKeyCodes input:text, .showKeyCodes textarea").css("border", "1px solid black").showKeyCodes();
});
//--></script>
</head>
<body>

    <form action="" method="get" class="showKeyCodes" size="20">
        <p><input type="text"></p>
        <p><textarea rows="3" cols="30"></textarea></p>
    </form>

</body>
</html>

. ( ) , . , , , .

+3
3

, , , , . , qwerty - 44, 46.

+1

e.which?

IE , keyCode ( keypress , key code). which, jQuery keyCode which IE. jQuery , , . ( keyCode keypress, , 0 Firefox.)

: : .

keypress, (46, ASCII .). keydown, key code ( keyCode, , IE, which).

keyCode keyCode ./, - 110 (ASCII N). , 78, ASCII N , , N.

, keydown , 110 78, keypress. keydown , which (charCode) 46, return false, , , . ( , , , .)

, ; . . IE (document.selection.createRange().text=...) ( value selectionStart/selectionEnd). , , .

- ? ?

, , . - -, - -; , .

, , , , , MSKLC.

+2

Some time after asking this question, I finally found a jQuery plugin that does just that: numpad-decimal-separator

0
source

Source: https://habr.com/ru/post/1754373/


All Articles