Skip to content

Instantly share code, notes, and snippets.

@mikechambers
Created May 7, 2012 21:53
Show Gist options
  • Save mikechambers/2630712 to your computer and use it in GitHub Desktop.
Save mikechambers/2630712 to your computer and use it in GitHub Desktop.
function _normalizeKeyDescriptorString(origDescriptor) {
var hasCtrl = false,
hasAlt = false,
hasShift = false,
key = "";
function _isModifier(left, right, previouslyFound) {
if (!left || !right) {
return false;
}
left = left.trim().toLowerCase();
right = right.trim().toLowerCase();
var matched = (left.length > 0 && left === right);
if (matched && previouslyFound) {
console.log("KeyMap _normalizeKeyDescriptorString() - Modifier defined twice: " + origDescriptor);
}
return matched;
}
origDescriptor.split("-").forEach(function parseDescriptor(ele, i, arr) {
if (_isModifier("ctrl", ele, hasCtrl)) {
hasCtrl = true;
} else if (_isModifier("cmd", ele, hasCtrl)) {
console.log("KeyMap _normalizeKeyDescriptorString() - Cmd getting mapped to Ctrl from: " + origDescriptor);
hasCtrl = true;
} else if (_isModifier("alt", ele, hasAlt)) {
hasAlt = true;
} else if (_isModifier("opt", ele, hasAlt)) {
console.log("KeyMap _normalizeKeyDescriptorString() - Opt getting mapped to Alt from: " + origDescriptor);
hasAlt = true;
} else if (_isModifier("shift", ele, hasShift)) {
hasShift = true;
} else if (key.length > 0) {
console.log("KeyMap _normalizeKeyDescriptorString() - Multiple keys defined. Using key: " + key + " from: " + origDescriptor);
} else {
key = ele;
}
});
return _buildKeyDescriptor(hasCtrl, hasAlt, hasShift, key);
}
function _isModifier(left, right, previouslyFound, origDescriptor) {
if (!left || !right) {
return false;
}
left = left.trim().toLowerCase();
right = right.trim().toLowerCase();
var matched = (left.length > 0 && left === right);
if (matched && previouslyFound) {
console.log("KeyMap _normalizeKeyDescriptorString() - Modifier defined twice: " + origDescriptor);
}
return matched;
}
function _normalizeKeyDescriptorString(origDescriptor) {
var hasCtrl = false,
hasAlt = false,
hasShift = false,
key = "";
origDescriptor.split("-").forEach(function parseDescriptor(ele, i, arr) {
if (_isModifier("ctrl", ele, hasCtrl)) {
hasCtrl = true;
} else if (_isModifier("cmd", ele, hasCtrl, this)) {
console.log("KeyMap _normalizeKeyDescriptorString() - Cmd getting mapped to Ctrl from: " + origDescriptor);
hasCtrl = true;
} else if (_isModifier("alt", ele, hasAlt, this)) {
hasAlt = true;
} else if (_isModifier("opt", ele, hasAlt, this)) {
console.log("KeyMap _normalizeKeyDescriptorString() - Opt getting mapped to Alt from: " + origDescriptor);
hasAlt = true;
} else if (_isModifier("shift", ele, hasShift, this)) {
hasShift = true;
} else if (key.length > 0) {
console.log("KeyMap _normalizeKeyDescriptorString() - Multiple keys defined. Using key: " + key + " from: " + origDescriptor);
} else {
key = ele;
}
});
return _buildKeyDescriptor(hasCtrl, hasAlt, hasShift, key);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment