Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

now using key event data codes from http://www.aspdotnetfaq.com/Faq/W…

  • Loading branch information...
commit 33c3da8cae3671b6e9337053caa7933f14195c36 1 parent c290636
@substack authored
Showing with 112 additions and 33 deletions.
  1. +81 −0 data/events.json
  2. +0 −23 data/shifted.json
  3. +31 −10 index.js
View
81 data/events.json
@@ -0,0 +1,81 @@
+{
+ "both" : {
+ "8" : "BackSpace",
+ "9" : "Tab",
+ "13" : "Return",
+ "16" : "Shift_L",
+ "17" : "Control_L",
+ "18" : "Alt_L",
+ "19" : "Pause",
+ "20" : "Caps_Lock",
+ "27" : "Escape",
+ "33" : "Prior",
+ "34" : "Next",
+ "35" : "End",
+ "36" : "Home",
+ "37" : "Left",
+ "38" : "Up",
+ "39" : "Right",
+ "40" : "Down",
+ "44" : "Print",
+ "45" : "Insert",
+ "46" : "Delete"
+ },
+ "shifted" : {
+ "96" : ")",
+ "97" : "!",
+ "98" : "@",
+ "99" : "#",
+ "100" : "$",
+ "101" : "%",
+ "102" : "^",
+ "103" : "&",
+ "104" : "*",
+ "105" : "(",
+ "107" : "+",
+ "109" : "_",
+ "110" : ">",
+ "188" : "<"
+ },
+ "unshifted" : {
+ "8" : "BackSpace",
+ "16" : "Shift_L",
+ "96" : "0",
+ "97" : "1",
+ "98" : "2",
+ "99" : "3",
+ "100" : "4",
+ "101" : "5",
+ "102" : "6",
+ "103" : "7",
+ "104" : "8",
+ "105" : "9",
+ "106" : "*",
+ "107" : "=",
+ "109" : "-",
+ "110" : ".",
+ "111" : "/",
+ "112" : "F1",
+ "113" : "F2",
+ "114" : "F3",
+ "115" : "F4",
+ "116" : "F5",
+ "117" : "F6",
+ "118" : "F7",
+ "119" : "F8",
+ "120" : "F9",
+ "121" : "F10",
+ "122" : "F11",
+ "123" : "F12",
+ "144" : "Num_Lock",
+ "145" : "Scroll_Lock",
+ "186" : ";",
+ "188" : ",",
+ "189" : "-",
+ "190" : ".",
+ "191" : "/",
+ "219" : "]",
+ "220" : "\\",
+ "222" : "'"
+ }
+}
View
23 data/shifted.json
@@ -1,23 +0,0 @@
-{
- "`" : "~",
- "1" : "!",
- "2" : "@",
- "3" : "#",
- "4" : "$",
- "5" : "%",
- "6" : "^",
- "7" : "&",
- "8" : "*",
- "9" : "(",
- "0" : ")",
- "-" : "_",
- "=" : "+",
- "[" : "{",
- "]" : "}",
- "\\" : "|",
- ";" : ":",
- "'" : "\"",
- "," : "<",
- "." : ">",
- "/" : "?"
-}
View
41 index.js
@@ -4,8 +4,8 @@ var data = JSON.parse(fs.readFileSync(
__dirname + '/data/keysyms.json', 'utf8'
));
-var shifted = JSON.parse(fs.readFileSync(
- __dirname + '/data/shifted.json', 'utf8'
+var events = JSON.parse(fs.readFileSync(
+ __dirname + '/data/events.json', 'utf8'
));
exports.records = data.records;
@@ -15,27 +15,48 @@ exports.fromKeysym = function (keysym) {
};
exports.fromUnicode = function (code) {
- return (data.unicodes[code] || []).map(function (i) {
- return data.records[i]
- });
+ if (typeof code === 'string') {
+ if (code.length !== 1) {
+ throw new Error('String must be 1 character');
+ }
+ return exports.fromUnicode(code.charCodeAt(0));
+ }
+ else {
+ return (data.unicodes[code] || []).map(function (i) {
+ return data.records[i]
+ });
+ }
};
var a = 'a'.charCodeAt(0), z = 'z'.charCodeAt(0);
var A = 'A'.charCodeAt(0), Z = 'Z'.charCodeAt(0);
+function lookup (e) {
+ return e.length === 1
+ ? exports.fromUnicode(e)[0].keysym
+ : exports.fromName(e).keysym
+ ;
+}
+
exports.keyEvent = function (code, shiftMask) {
if (a <= code && code <= z) {
- return code - (shiftMask ? A - a : 0);
+ return code - (shiftMask ? a - A : 0);
}
else if (A <= code && code <= Z) {
- return code + (shiftMask ? 0 : A - a);
+ return code + (shiftMask ? 0 : a - A);
+ }
+ else if (events.both[code]) {
+ return lookup(events.both[code]);
+ }
+ else if (shiftMask && events.shifted[code]) {
+ return lookup(events.shifted[code]);
}
- else if (shifted[code]) {
- return String.fromCharCode(shifted[code]);
+ else if (!shiftMask && events.unshifted[code]) {
+ return lookup(events.unshifted[code]);
}
else {
var c = exports.fromUnicode(code)[0];
- return c && c.keysym;
+ return c && c.keysym || code;
}
};
Please sign in to comment.
Something went wrong with that request. Please try again.