Skip to content

Commit

Permalink
feat(keycode): add code property to parse result (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
c4spar committed Apr 17, 2021
1 parent b9eb39b commit 366683f
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 1 deletion.
2 changes: 2 additions & 0 deletions keycode/key_code.ts
Expand Up @@ -81,6 +81,7 @@ export class KeyCode {
ctrl: false,
meta: false,
shift: false,
code: undefined,
};

if (ch === kEscape && hasNext()) {
Expand Down Expand Up @@ -191,6 +192,7 @@ export class KeyCode {
key.ctrl = !!(modifier & 4);
key.meta = !!(modifier & 10);
key.shift = !!(modifier & 1);
key.code = code;

// Parse the key itself
if (code in KeyMap) {
Expand Down
3 changes: 3 additions & 0 deletions keycode/key_event.ts
Expand Up @@ -2,6 +2,7 @@
export interface IKey {
name?: string;
sequence?: string;
code?: string;
ctrl: boolean;
meta: boolean;
shift: boolean;
Expand All @@ -12,6 +13,7 @@ export class KeyEvent {
protected constructor(
public readonly name: string | undefined,
public readonly sequence: string | undefined,
public readonly code: string | undefined,
public readonly ctrl = false,
public readonly meta = false,
public readonly shift = false,
Expand All @@ -25,6 +27,7 @@ export class KeyEvent {
return new this(
key.name,
key.sequence,
key.code,
key.ctrl,
key.meta,
key.shift,
Expand Down
118 changes: 117 additions & 1 deletion keycode/test/key_code_test.ts
Expand Up @@ -11,8 +11,9 @@ import type { IKey, KeyEvent } from "../key_event.ts";
const ESC = "\x1B";

const defaults = <IKey> {
sequence: undefined,
name: undefined,
sequence: undefined,
code: undefined,
ctrl: false,
meta: false,
shift: false,
Expand Down Expand Up @@ -94,6 +95,7 @@ for (const code of Object.keys(KeyMap)) {
...defaults,
name,
sequence: ESC + code,
code,
}],
);
});
Expand All @@ -111,6 +113,7 @@ for (const code of Object.keys(KeyMapShift)) {
...defaults,
name,
sequence: ESC + code,
code,
shift: true,
}],
);
Expand All @@ -129,6 +132,7 @@ for (const code of Object.keys(KeyMapCtrl)) {
...defaults,
name,
sequence: ESC + code,
code,
ctrl: true,
}],
);
Expand All @@ -155,3 +159,115 @@ Deno.test(`keycode - parse string - abc`, function () {
}],
);
});

Deno.test("keycode - xterm/gnome ESC [ letter (with modifiers)", function () {
const keys = KeyCode.parse(
"\x1b[2P\x1b[3P\x1b[4P\x1b[5P\x1b[6P\x1b[7P\x1b[8P\x1b[3Q\x1b[8Q\x1b[3R\x1b[8R\x1b[3S\x1b[8S",
);
assertEquals(keys, [
{
name: "f1",
sequence: "\x1b[2P",
code: "[P",
shift: true,
meta: false,
ctrl: false,
},
{
name: "f1",
sequence: "\x1b[3P",
code: "[P",
shift: false,
meta: true,
ctrl: false,
},
{
name: "f1",
sequence: "\x1b[4P",
code: "[P",
shift: true,
meta: true,
ctrl: false,
},
{
name: "f1",
sequence: "\x1b[5P",
code: "[P",
shift: false,
meta: false,
ctrl: true,
},
{
name: "f1",
sequence: "\x1b[6P",
code: "[P",
shift: true,
meta: false,
ctrl: true,
},
{
name: "f1",
sequence: "\x1b[7P",
code: "[P",
shift: false,
meta: true,
ctrl: true,
},
{
name: "f1",
sequence: "\x1b[8P",
code: "[P",
shift: true,
meta: true,
ctrl: true,
},
{
name: "f2",
sequence: "\x1b[3Q",
code: "[Q",
shift: false,
meta: true,
ctrl: false,
},
{
name: "f2",
sequence: "\x1b[8Q",
code: "[Q",
shift: true,
meta: true,
ctrl: true,
},
{
name: "f3",
sequence: "\x1b[3R",
code: "[R",
shift: false,
meta: true,
ctrl: false,
},
{
name: "f3",
sequence: "\x1b[8R",
code: "[R",
shift: true,
meta: true,
ctrl: true,
},
{
name: "f4",
sequence: "\x1b[3S",
code: "[S",
shift: false,
meta: true,
ctrl: false,
},
{
name: "f4",
sequence: "\x1b[8S",
code: "[S",
shift: true,
meta: true,
ctrl: true,
},
]);
});

0 comments on commit 366683f

Please sign in to comment.