Skip to content

Commit

Permalink
refine KeyboardUtils.getKeyChar
Browse files Browse the repository at this point in the history
  • Loading branch information
brookhong committed Mar 10, 2016
1 parent 5dfc883 commit 8c60b0b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 19 deletions.
2 changes: 1 addition & 1 deletion content_scripts/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ var Events = (function() {
self.toggleBlacklist(window.location.origin);
return;
}
if (isExcluded(event.target)) {
if (isExcluded(event.target) || key === "") {
return;
}
delete self.focusHandlers.getBackFocusOnLoad;
Expand Down
3 changes: 2 additions & 1 deletion content_scripts/normal.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,11 @@ var Normal = (function() {
return $(help_groups);
}

self.highlightElement = function(sn) {
self.highlightElement = function(sn, duration) {
var rc = sn.getBoundingClientRect();
runtime.frontendCommand({
action: 'highlightElement',
duration: duration || 200,
rect: {
top: rc.top,
left: rc.left,
Expand Down
44 changes: 28 additions & 16 deletions content_scripts/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ String.prototype.format = function() {
downArrow: 40,
upArrow: 38
},
modifierKeys: {
16: "Shift",
17: "Ctrl",
18: "Alt"
},
keyNames: {
8: 'Backspace',
9: 'Tab',
Expand Down Expand Up @@ -102,27 +107,34 @@ String.prototype.format = function() {
},
getKeyChar: function(event) {
var character, correctedIdentifiers, keyIdentifier, unicodeKeyInHex;
if (event.keyIdentifier.slice(0, 2) !== "U+") {
character = "<{0}>".format(event.keyIdentifier);
} else if (this.keyNames.hasOwnProperty(event.keyCode)) {
character = "<{0}>".format(this.keyNames[event.keyCode]);
if (event.keyCode in this.modifierKeys) {
character = "";
} else {
keyIdentifier = event.keyIdentifier;
if ((this.platform === "Windows" || this.platform === "Linux") && this.keyIdentifierCorrectionMap[keyIdentifier]) {
correctedIdentifiers = this.keyIdentifierCorrectionMap[keyIdentifier];
keyIdentifier = event.shiftKey ? correctedIdentifiers[1] : correctedIdentifiers[0];
if (event.keyIdentifier.slice(0, 2) !== "U+") {
character = "{0}".format(event.keyIdentifier);
} else if (this.keyNames.hasOwnProperty(event.keyCode)) {
character = "{0}".format(this.keyNames[event.keyCode]);
} else {
keyIdentifier = event.keyIdentifier;
if ((this.platform === "Windows" || this.platform === "Linux") && this.keyIdentifierCorrectionMap[keyIdentifier]) {
correctedIdentifiers = this.keyIdentifierCorrectionMap[keyIdentifier];
keyIdentifier = event.shiftKey ? correctedIdentifiers[1] : correctedIdentifiers[0];
}
unicodeKeyInHex = "0x" + keyIdentifier.substring(2);
character = String.fromCharCode(parseInt(unicodeKeyInHex));
character = event.shiftKey ? character : character.toLowerCase();
}
unicodeKeyInHex = "0x" + keyIdentifier.substring(2);
character = String.fromCharCode(parseInt(unicodeKeyInHex));
character = event.shiftKey ? character : character.toLowerCase();
if (event.ctrlKey) {
character = "<Ctrl-{0}>".format(character);
if (event.metaKey) {
character = "Meta-" + character;
}
if (event.altKey) {
character = "<Alt-{0}>".format(character);
character = "Alt-" + character;
}
if (event.metaKey) {
character = "<Meta-{0}>".format(character);
if (event.ctrlKey) {
character = "Ctrl-" + character;
}
if (character.length > 1) {
character = "<{0}>".format(character);
}
}
return character;
Expand Down
2 changes: 1 addition & 1 deletion pages/omnibar.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ var frontendUI = (function() {
setTimeout(function() {
frameElement.hide();
self.flush();
}, 200);
}, message.duration);
};

_tabs.onShow = function(tabs) {
Expand Down

0 comments on commit 8c60b0b

Please sign in to comment.