Skip to content
This repository has been archived by the owner on May 21, 2019. It is now read-only.

Commit

Permalink
Use UI Events key instead of keyCode in Job.write. Fixes #351, fixes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
vlad-shatskyi committed Jun 14, 2016
1 parent d54e62f commit 3b641c9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 119 deletions.
11 changes: 2 additions & 9 deletions src/Job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {PluginManager} from "./PluginManager";
import {EmitterWithUniqueID} from "./EmitterWithUniqueID";
import {Status} from "./Enums";
import {Environment} from "./Environment";
import {convertKeyCode} from "./utils/Common";
import {normalizeKey} from "./utils/Common";
import {TerminalLikeDevice} from "./Interfaces";

function makeThrottledDataEmitter(timesPerSecond: number, subject: EmitterWithUniqueID) {
Expand Down Expand Up @@ -75,14 +75,7 @@ export class Job extends EmitterWithUniqueID implements TerminalLikeDevice {
if (typeof input === "string") {
text = input;
} else {
const event = <KeyboardEvent>(<any>input).nativeEvent;
let code = event.keyCode;

if (event.ctrlKey) {
text = String.fromCharCode(code - 64);
} else {
text = convertKeyCode(code, event.shiftKey);
}
text = input.ctrlKey ? String.fromCharCode(input.keyCode - 64) : normalizeKey(input.key);
}

this.command.write(text);
Expand Down
122 changes: 12 additions & 110 deletions src/utils/Common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,126 +241,28 @@ export function groupWhen<T>(grouper: (a: T, b: T) => boolean, row: T[]): T[][]


/**
* @link https://lists.w3.org/Archives/Public/www-dom/2010JulSep/att-0182/keyCode-spec.html
* @link http://unixpapa.com/js/key.html
* @link http://www.cambiaresearch.com/articles/15/javascript-key-codes
* @link https://www.w3.org/TR/uievents/#widl-KeyboardEvent-key
*/
export function convertKeyCode(keyCode: number, shift: boolean): string {
switch (keyCode) {
case KeyCode.Backspace:
export function normalizeKey(key: string): string {
switch (key) {
case "Backspace":
return String.fromCharCode(127);
case KeyCode.Tab:
case "Tab":
return String.fromCharCode(KeyCode.Tab);
case KeyCode.CarriageReturn:
case "Enter":
return String.fromCharCode(KeyCode.CarriageReturn);
case KeyCode.Escape:
case "Escape":
return String.fromCharCode(KeyCode.Escape);
case KeyCode.Space:
return " ";
case KeyCode.Left:
case "ArrowLeft":
return "\x1b[D";
case KeyCode.Up:
case "ArrowUp":
return "\x1b[A";
case KeyCode.Right:
case "ArrowRight":
return "\x1b[C";
case KeyCode.Down:
case "ArrowDown":
return "\x1b[B";
case 48:
return shift ? ")" : "0";
case 49:
return shift ? "!" : "1";
case 50:
return shift ? "@" : "2";
case 51:
return shift ? "#" : "3";
case 52:
return shift ? "$" : "4";
case 53:
return shift ? "%" : "5";
case 54:
return shift ? "^" : "6";
case 55:
return shift ? "&" : "7";
case 56:
return shift ? "*" : "8";
case 57:
return shift ? "(" : "9";
case 65:
return shift ? "A" : "a";
case 66:
return shift ? "B" : "b";
case 67:
return shift ? "C" : "c";
case 68:
return shift ? "D" : "d";
case 69:
return shift ? "E" : "e";
case 70:
return shift ? "F" : "f";
case 71:
return shift ? "G" : "g";
case 72:
return shift ? "H" : "h";
case 73:
return shift ? "I" : "i";
case 74:
return shift ? "J" : "j";
case 75:
return shift ? "K" : "k";
case 76:
return shift ? "L" : "l";
case 77:
return shift ? "M" : "m";
case 78:
return shift ? "N" : "n";
case 79:
return shift ? "O" : "o";
case 80:
return shift ? "P" : "p";
case 81:
return shift ? "Q" : "q";
case 82:
return shift ? "R" : "r";
case 83:
return shift ? "S" : "s";
case 84:
return shift ? "T" : "t";
case 85:
return shift ? "U" : "u";
case 86:
return shift ? "V" : "v";
case 87:
return shift ? "W" : "w";
case 88:
return shift ? "X" : "x";
case 89:
return shift ? "Y" : "y";
case 90:
return shift ? "Z" : "z";
case 186:
return shift ? ":" : ";";
case 187:
return shift ? "+" : "=";
case 188:
return shift ? "<" : ",";
case 189:
return shift ? "_" : "-";
case 190:
return shift ? ">" : ".";
case 191:
return shift ? "?" : "/";
case 192:
return shift ? "~" : "`";
case 219:
return shift ? "{" : "[";
case 220:
return shift ? "|" : "\\";
case 221:
return shift ? "}" : "]";
case 222:
return shift ? "\"" : "'";
default:
throw `Unknown key code: ${keyCode}`;
return key;
}
}

Expand Down

0 comments on commit 3b641c9

Please sign in to comment.