0

我想知道哪些数字代表键盘上的哪些键,例如,我知道 65 代表a,但哪个数字代表s

我在任何地方都找不到它。如果我没有按应有的方式进行搜索,请重定向我。

提前致谢。

4

3 回答 3

2

好吧,我有几分钟的时间,所以我写了这个(它应该允许你在键盘上找到任何给定键的键码):

function keyMap(start, stop) {
    var startFrom, stopAt, o = {};

    // doing different things, depending on what the 'start' variable is:
    switch (typeof start) {
        // if it's a string, we need the character-code, so we get that:
        case 'string':
            startFrom = start.charCodeAt(0);
            break;
        // if it's already a number, we use that as-is:
        case 'number':
            startFrom = start;
            break;
        // whatever else it might be, we quit here:
        default:
            return '';
    }

    // similarly for the 'stop' variable:
    switch (typeof stop) {
        case 'string':
            stopAt = stop.charCodeAt(0);
            break;
        case 'number':
            stopAt = stop;
            break;
        // if it's neither a number, nor a string,
        default:
            /* but start has a length of at least 2, and start is a string,
               we use the second character of the start string, or
               we simply add 1 to the character-code from the start variable: */
            stopAt = start.length > 1 && typeof start === 'string' ? start.charCodeAt(1) : startFrom;
            break;
    }

    /* iterate over the character-codes (using 'len = stopAt + 1 because we
       want to include the ending character): */
    for (var i = startFrom, len = stopAt + 1; i < len; i++) {
        // setting the keys of the 'o' map, and the value stored therein:
        o[String.fromCharCode(i)] = i;
    }
    return o;
}

var map = keyMap('s');
console.log(map, map['s'], map.s);

JS 小提琴演示

或者,要查找一系列键码:

var map = keyMap('a','z');
console.log(map, map.a, map.b, map.c /* ...and so on... */);

JS 小提琴演示

或者,要找到相同的范围,但只提供一个参数:

var map = keyMap('az');
console.log(map, map.a, map.b, map.c /* ...and so on... */);

JS 小提琴演示

参考:

于 2013-10-19T19:16:13.173 回答
1

在此处查看 ascii 表中的十进制值。小写s为 115。

于 2013-10-19T17:47:43.887 回答
0

是所有keyCodes的链接..但是你可以自己尝试..

使用 jQuery

$(document).keyup(function(e){
  alert(e.keyCode);
  //or
  alert(e.which);
})

在这里摆弄

于 2013-10-19T17:50:20.060 回答