Skip to content

Instantly share code, notes, and snippets.

@fragsalat
Created May 17, 2016 12:56
Show Gist options
  • Save fragsalat/61e30685b5f25044c273ecd288ff26d2 to your computer and use it in GitHub Desktop.
Save fragsalat/61e30685b5f25044c273ecd288ff26d2 to your computer and use it in GitHub Desktop.
export class CharCode {
static KEYS = {
'219': 223, // ß
'186': 246, // ö
'222': 228, // ä
'188': 44, // ,
'190': 46, // .
'189': 45, // -
'187': 43, // +
'191': 35, // #
'229': 94 // ^
};
static SHIFT_KEYS = {
'49': 33, // !
'50': 34, // "
'51': 167, // §
'52': 36, // $
'53': 37, // %
'54': 38, // &
'55': 47, // /
'56': 40, // (
'57': 41, // )
'48': 61, // =
'219': 63, // ?
'187': 42, // *
'191': 39, // '
'188': 59, // ;
'190': 58, // :
'189': 95, // _
'192': 176, // °
'220': 62 // >
};
static ALT_KEYS = {
'49': 185, // ¹
'50': 178, // ²
'51': 179, // ³
'52': 188, // ¼
'53': 189, // ½
'54': 175, // ¬
'55': 123, // {
'56': 91, // [
'57': 93, // ]
'48': 125, // }
'219': 92, // \
'81': 64, // @
'220': 124, // |
'187': 126 // ~
};
static NUM_PAD_KEYS = {
'96': 48, // 0
'97': 49, // 1
'98': 50, // 2
'99': 51, // 3
'100': 52, // 4
'101': 53, // 5
'102': 54, // 6
'103': 55, // 7
'104': 56, // 8
'105': 57, // 9
'106': 42, // *
'107': 43, // +
'108': 44, // ,
'109': 45, // -
'111': 47 // /
};
/**
* Get char code from event
*
* @param {object} event JavaScript Keyboard event
* @returns {number}
*/
static get(event) {
if (event.charCode) {
return event.charCode;
}
var code = event.keyCode || event.which;
// Map keys pressed with shift key to char
if (event.shiftKey && CharCode.SHIFT_KEYS[code] !== undefined) {
return CharCode.SHIFT_KEYS[code];
}
// Map keys pressed with alt gr key to char
if (event.altKey && CharCode.ALT_KEYS[code] !== undefined) {
return CharCode.ALT_KEYS[code];
}
// Map num pad keys to char
if (CharCode.NUM_PAD_KEYS[code] !== undefined) {
return CharCode.NUM_PAD_KEYS[code];
}
// Map general keys to char value
if (CharCode.KEYS[code] !== undefined) {
return CharCode.KEYS[code];
}
// Fix alphabetic keys without shift key to lowercase
if (code >= 65 && code <= 90 && !event.shiftKey) {
return code + 32;
}
return code;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment