Skip to content

Commit

Permalink
fix: arrow keys mapping to keycodes
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed Jan 12, 2022
1 parent 97c2b79 commit a772d84
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
7 changes: 7 additions & 0 deletions projects/ngneat/hotkeys/src/lib/tests/hotkeys.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,13 @@ describe('Service: Hotkeys', () => {
]
});
});

it('should listen to up', () => {
const spyFcn = createSpy('subscribe', e => {});
spectator.service.addShortcut({ keys: 'up' }).subscribe(spyFcn);
fakeKeyboardPress('ArrowUp');
expect(spyFcn).toHaveBeenCalled();
});
});

function fakeKeyboardPress(key: string, type = 'keydown') {
Expand Down
32 changes: 23 additions & 9 deletions projects/ngneat/hotkeys/src/lib/utils/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,28 @@ export function hostPlatform(): Platform {
}

export function normalizeKeys(keys: string, platform: Platform): string {
const lowercaseKeys = keys.toLowerCase();
switch (platform) {
case 'pc':
return lowercaseKeys
.split('.')
.map(k => (k === 'meta' ? 'control' : k))
.join('.');
default:
return keys;
const transformMap = {
up: 'ArrowUp',
down: 'ArrowDown',
left: 'ArrowLeft',
right: 'ArrowRight'
};

function transform(key: string): string {
if (platform === 'pc' && key === 'meta') {
key = 'control';
}

if (key in transformMap) {
key = transformMap[key];
}

return key;
}

return keys
.toLowerCase()
.split('.')
.map(transform)
.join('.');
}

0 comments on commit a772d84

Please sign in to comment.