我想知道哪些数字代表键盘上的哪些键,例如,我知道 65 代表a
,但哪个数字代表s
?
我在任何地方都找不到它。如果我没有按应有的方式进行搜索,请重定向我。
提前致谢。
好吧,我有几分钟的时间,所以我写了这个(它应该允许你在键盘上找到任何给定键的键码):
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);
或者,要查找一系列键码:
var map = keyMap('a','z');
console.log(map, map.a, map.b, map.c /* ...and so on... */);
或者,要找到相同的范围,但只提供一个参数:
var map = keyMap('az');
console.log(map, map.a, map.b, map.c /* ...and so on... */);
参考:
在此处查看 ascii 表中的十进制值。小写s
为 115。