Skip to content

Commit

Permalink
Add meta key
Browse files Browse the repository at this point in the history
  • Loading branch information
Vadim Demedes committed Sep 23, 2019
1 parent 1bdd495 commit 468e33b
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export interface Key {
escape: boolean
ctrl: boolean
shift: boolean
meta: boolean
}

/**
Expand Down
7 changes: 7 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,13 @@ Ctrl key was pressed.
Type: `boolean`<br>
Default: `false`
##### key.meta
Type: `boolean`<br>
Default: `false`
[Meta key](https://en.wikipedia.org/wiki/Meta_key) was pressed.
Shift key was pressed.
## Useful Components
Expand Down
8 changes: 7 additions & 1 deletion src/hooks/use-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export default inputHandler => {
return: input === '\r',
escape: input === '\u001B',
ctrl: false,
shift: false
shift: false,
meta: false
};

// Copied from `keypress` module
Expand All @@ -32,6 +33,11 @@ export default inputHandler => {
key.ctrl = true;
}

if (input[0] === '\u001B') {
input = input.slice(1);
key.meta = true;
}

const isLatinUppercase = input >= 'A' && input <= 'Z';
const isCyrillicUppercase = input >= 'А' && input <= 'Я';
if (input.length === 1 && (isLatinUppercase || isCyrillicUppercase)) {
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/use-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ const UserInput = ({test}) => {
return;
}

if (test === 'meta' && input === 'm' && key.meta) {
exit();
return;
}

if (test === 'upArrow' && key.upArrow) {
exit();
return;
Expand Down
7 changes: 7 additions & 0 deletions test/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ test('handle ctrl', async t => {
t.true(ps.output.includes('exited'));
});

test('handle meta', async t => {
const ps = term('use-input', ['meta']);
ps.write('\u001Bm');
await ps.waitForExit();
t.true(ps.output.includes('exited'));
});

test('handle up arrow', async t => {
const ps = term('use-input', ['upArrow']);
ps.write('\u001B[A');
Expand Down

0 comments on commit 468e33b

Please sign in to comment.