Permalink
Browse files

Break out specialKey and delKey recognition into keyPress and keyDown…

… methods.
1 parent 1628fd7 commit 99f4366bf5395f8b9e569e7cfbf7e7fbabc18f5e @jaridmargolin jaridmargolin committed May 9, 2014
View
@@ -1,11 +1,11 @@
{
"name": "formatter.js",
- "version": "0.1.2",
+ "version": "0.1.3",
"devDependencies": {
"easy-amdtest": "~0.0.1",
"requirejs": "~2.1.11",
"jquery": "~1.11.0",
- "fakey": "~0.0.7"
+ "fakey": "~0.0.8"
},
"ignore": [
"**/.*",
View
@@ -160,7 +160,7 @@ Formatter.prototype._keyDown = function (evt) {
var k = evt.which || evt.keyCode;
// If delete key
- if (k && utils.isDelKey(k)) {
+ if (k && utils.isDelKeyDown(evt.which, evt.keyCode)) {
// Process the keyCode and prevent default
this._processKey(null, k);
return utils.preventDefault(evt);
@@ -177,14 +177,11 @@ Formatter.prototype._keyPress = function (evt) {
var k, isSpecial;
// Mozilla will trigger on special keys and assign the the value 0
// We want to use that 0 rather than the keyCode it assigns.
- if (evt.which) {
- k = evt.which;
- } else {
- k = evt.keyCode;
- isSpecial = utils.isSpecialKey(k);
- }
+ k = evt.which || evt.keyCode;
+ isSpecial = utils.isSpecialKeyPress(evt.which, evt.keyCode);
+
// Process the keyCode and prevent default
- if (!utils.isDelKey(k) && !isSpecial && !utils.isModifier(evt)) {
+ if (!utils.isDelKeyPress(evt.which, evt.keyCode) && !isSpecial && !utils.isModifier(evt)) {
this._processKey(String.fromCharCode(k), false);
return utils.preventDefault(evt);
}
View
@@ -13,8 +13,7 @@ define(function () {
var utils = {};
// Useragent info for keycode handling
-var uAgent = (typeof navigator !== 'undefined') ? navigator.userAgent : null,
- iPhone = /iphone/i.test(uAgent);
+var uAgent = (typeof navigator !== 'undefined') ? navigator.userAgent : null;
//
// Shallow copy properties from n objects to destObj
@@ -76,29 +75,78 @@ utils.getClip = function (evt) {
};
//
-// Returns true/false if k is a del key
+// Loop over object and checking for matching properties
//
-utils.isDelKey = function (k) {
- return k === 8 || k === 46 || (iPhone && k === 127);
+utils.getMatchingKey = function (which, keyCode, keys) {
+ // Loop over and return if matched.
+ for (var k in keys) {
+ var key = keys[k];
+ if (which === key.which && keyCode === key.keyCode) {
+ return k;
+ }
+ }
};
//
-// Returns true/false if k is an arrow key
+// Returns true/false if k is a del keyDown
//
-utils.isSpecialKey = function (k) {
- var codes = {
- '9' : 'tab',
- '13': 'enter',
- '35': 'end',
- '36': 'home',
- '37': 'leftarrow',
- '38': 'uparrow',
- '39': 'rightarrow',
- '40': 'downarrow',
- '116': 'F5'
+utils.isDelKeyDown = function (which, keyCode) {
+ var keys = {
+ 'backspace': { 'which': 8, 'keyCode': 8 },
+ 'delete': { 'which': 46, 'keyCode': 46 }
};
- // If del or special key
- return codes[k];
+
+ return utils.getMatchingKey(which, keyCode, keys);
+};
+
+//
+// Returns true/false if k is a del keyPress
+//
+utils.isDelKeyPress = function (which, keyCode) {
+ var keys = {
+ 'backspace': { 'which': 8, 'keyCode': 8, 'shiftKey': false },
+ 'delete': { 'which': 0, 'keyCode': 46 }
+ };
+
+ return utils.getMatchingKey(which, keyCode, keys);
+};
+
+// //
+// // Determine if keydown relates to specialKey
+// //
+// utils.isSpecialKeyDown = function (which, keyCode) {
+// var keys = {
+// 'tab': { 'which': 9, 'keyCode': 9 },
+// 'enter': { 'which': 13, 'keyCode': 13 },
+// 'end': { 'which': 35, 'keyCode': 35 },
+// 'home': { 'which': 36, 'keyCode': 36 },
+// 'leftarrow': { 'which': 37, 'keyCode': 37 },
+// 'uparrow': { 'which': 38, 'keyCode': 38 },
+// 'rightarrow': { 'which': 39, 'keyCode': 39 },
+// 'downarrow': { 'which': 40, 'keyCode': 40 },
+// 'F5': { 'which': 116, 'keyCode': 116 }
+// };
+
+// return utils.getMatchingKey(which, keyCode, keys);
+// };
+
+//
+// Determine if keypress relates to specialKey
+//
+utils.isSpecialKeyPress = function (which, keyCode) {
+ var keys = {
+ 'tab': { 'which': 0, 'keyCode': 9 },
+ 'enter': { 'which': 13, 'keyCode': 13 },
+ 'end': { 'which': 0, 'keyCode': 35 },
+ 'home': { 'which': 0, 'keyCode': 36 },
+ 'leftarrow': { 'which': 0, 'keyCode': 37 },
+ 'uparrow': { 'which': 0, 'keyCode': 38 },
+ 'rightarrow': { 'which': 0, 'keyCode': 39 },
+ 'downarrow': { 'which': 0, 'keyCode': 40 },
+ 'F5': { 'which': 116, 'keyCode': 116 }
+ };
+
+ return utils.getMatchingKey(which, keyCode, keys);
};
//
View
@@ -158,7 +158,7 @@ Formatter.prototype._keyDown = function (evt) {
var k = evt.which || evt.keyCode;
// If delete key
- if (k && utils.isDelKey(k)) {
+ if (k && utils.isDelKeyDown(evt.which, evt.keyCode)) {
// Process the keyCode and prevent default
this._processKey(null, k);
return utils.preventDefault(evt);
@@ -175,14 +175,11 @@ Formatter.prototype._keyPress = function (evt) {
var k, isSpecial;
// Mozilla will trigger on special keys and assign the the value 0
// We want to use that 0 rather than the keyCode it assigns.
- if (evt.which) {
- k = evt.which;
- } else {
- k = evt.keyCode;
- isSpecial = utils.isSpecialKey(k);
- }
+ k = evt.which || evt.keyCode;
+ isSpecial = utils.isSpecialKeyPress(evt.which, evt.keyCode);
+
// Process the keyCode and prevent default
- if (!utils.isDelKey(k) && !isSpecial && !utils.isModifier(evt)) {
+ if (!utils.isDelKeyPress(evt.which, evt.keyCode) && !isSpecial && !utils.isModifier(evt)) {
this._processKey(String.fromCharCode(k), false);
return utils.preventDefault(evt);
}
View
@@ -13,8 +13,7 @@
var utils = {};
// Useragent info for keycode handling
-var uAgent = (typeof navigator !== 'undefined') ? navigator.userAgent : null,
- iPhone = /iphone/i.test(uAgent);
+var uAgent = (typeof navigator !== 'undefined') ? navigator.userAgent : null;
//
// Shallow copy properties from n objects to destObj
@@ -76,29 +75,78 @@ utils.getClip = function (evt) {
};
//
-// Returns true/false if k is a del key
+// Loop over object and checking for matching properties
//
-utils.isDelKey = function (k) {
- return k === 8 || k === 46 || (iPhone && k === 127);
+utils.getMatchingKey = function (which, keyCode, keys) {
+ // Loop over and return if matched.
+ for (var k in keys) {
+ var key = keys[k];
+ if (which === key.which && keyCode === key.keyCode) {
+ return k;
+ }
+ }
};
//
-// Returns true/false if k is an arrow key
+// Returns true/false if k is a del keyDown
//
-utils.isSpecialKey = function (k) {
- var codes = {
- '9' : 'tab',
- '13': 'enter',
- '35': 'end',
- '36': 'home',
- '37': 'leftarrow',
- '38': 'uparrow',
- '39': 'rightarrow',
- '40': 'downarrow',
- '116': 'F5'
+utils.isDelKeyDown = function (which, keyCode) {
+ var keys = {
+ 'backspace': { 'which': 8, 'keyCode': 8 },
+ 'delete': { 'which': 46, 'keyCode': 46 }
};
- // If del or special key
- return codes[k];
+
+ return utils.getMatchingKey(which, keyCode, keys);
+};
+
+//
+// Returns true/false if k is a del keyPress
+//
+utils.isDelKeyPress = function (which, keyCode) {
+ var keys = {
+ 'backspace': { 'which': 8, 'keyCode': 8, 'shiftKey': false },
+ 'delete': { 'which': 0, 'keyCode': 46 }
+ };
+
+ return utils.getMatchingKey(which, keyCode, keys);
+};
+
+// //
+// // Determine if keydown relates to specialKey
+// //
+// utils.isSpecialKeyDown = function (which, keyCode) {
+// var keys = {
+// 'tab': { 'which': 9, 'keyCode': 9 },
+// 'enter': { 'which': 13, 'keyCode': 13 },
+// 'end': { 'which': 35, 'keyCode': 35 },
+// 'home': { 'which': 36, 'keyCode': 36 },
+// 'leftarrow': { 'which': 37, 'keyCode': 37 },
+// 'uparrow': { 'which': 38, 'keyCode': 38 },
+// 'rightarrow': { 'which': 39, 'keyCode': 39 },
+// 'downarrow': { 'which': 40, 'keyCode': 40 },
+// 'F5': { 'which': 116, 'keyCode': 116 }
+// };
+
+// return utils.getMatchingKey(which, keyCode, keys);
+// };
+
+//
+// Determine if keypress relates to specialKey
+//
+utils.isSpecialKeyPress = function (which, keyCode) {
+ var keys = {
+ 'tab': { 'which': 0, 'keyCode': 9 },
+ 'enter': { 'which': 13, 'keyCode': 13 },
+ 'end': { 'which': 0, 'keyCode': 35 },
+ 'home': { 'which': 0, 'keyCode': 36 },
+ 'leftarrow': { 'which': 0, 'keyCode': 37 },
+ 'uparrow': { 'which': 0, 'keyCode': 38 },
+ 'rightarrow': { 'which': 0, 'keyCode': 39 },
+ 'downarrow': { 'which': 0, 'keyCode': 40 },
+ 'F5': { 'which': 116, 'keyCode': 116 }
+ };
+
+ return utils.getMatchingKey(which, keyCode, keys);
};
//
Oops, something went wrong.

0 comments on commit 99f4366

Please sign in to comment.