I wondered which numbers represent which keys on your keyboard, for example, I know 65 represents a
, but which number would represents s
?
I couldn't find it anywhere. If I didn't search like I should have, please redirect me.
Thanks in advance.
I wondered which numbers represent which keys on your keyboard, for example, I know 65 represents a
, but which number would represents s
?
I couldn't find it anywhere. If I didn't search like I should have, please redirect me.
Thanks in advance.
Well, I had a few minutes and so I wrote this (which should allow you to find the key-code of any given key, or keys, on your keyboard):
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);
Or, to find a range of keycodes:
var map = keyMap('a','z');
console.log(map, map.a, map.b, map.c /* ...and so on... */);
Or, to find the same range, but with only one supplied argument:
var map = keyMap('az');
console.log(map, map.a, map.b, map.c /* ...and so on... */);
References:
Look at the Decimal values in The ascii table HERE. Lower-case s
is 115.
here is the link to all the keyCodes.. however you can always try it yourself ..
using jquery
$(document).keyup(function(e){
alert(e.keyCode);
//or
alert(e.which);
})