From a772d843efdb2ff7775e18f3a4aa6e83f6320d4e Mon Sep 17 00:00:00 2001 From: soyuka Date: Wed, 12 Jan 2022 11:29:48 +0100 Subject: [PATCH] fix: arrow keys mapping to keycodes --- .../src/lib/tests/hotkeys.service.spec.ts | 7 ++++ .../ngneat/hotkeys/src/lib/utils/platform.ts | 32 +++++++++++++------ 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/projects/ngneat/hotkeys/src/lib/tests/hotkeys.service.spec.ts b/projects/ngneat/hotkeys/src/lib/tests/hotkeys.service.spec.ts index 4cbcaa5..86a2b75 100644 --- a/projects/ngneat/hotkeys/src/lib/tests/hotkeys.service.spec.ts +++ b/projects/ngneat/hotkeys/src/lib/tests/hotkeys.service.spec.ts @@ -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') { diff --git a/projects/ngneat/hotkeys/src/lib/utils/platform.ts b/projects/ngneat/hotkeys/src/lib/utils/platform.ts index fdc9f10..7ef6d5d 100644 --- a/projects/ngneat/hotkeys/src/lib/utils/platform.ts +++ b/projects/ngneat/hotkeys/src/lib/utils/platform.ts @@ -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('.'); }