Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly handle special char keyCodes with Shift #1288

Merged
merged 1 commit into from
Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions addon-test-support/@ember/test-helpers/dom/trigger-key-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,29 @@ const keyFromKeyCode: { [key: number]: string } = {
222: "'",
};

const keyFromKeyCodeWithShift: { [key: number]: string } = {
48: ')',
49: '!',
50: '@',
51: '#',
52: '$',
53: '%',
54: '^',
55: '&',
56: '*',
57: '(',
186: ':',
187: '+',
188: '<',
189: '_',
190: '>',
191: '?',
219: '{',
220: '|',
221: '}',
222: '"',
};

/**
Calculates the value of KeyboardEvent#key given a keycode and the modifiers.
Note that this works if the key is pressed in combination with the shift key, but it cannot
Expand All @@ -119,10 +142,11 @@ function keyFromKeyCodeAndModifiers(
return String.fromCharCode(keycode).toLocaleLowerCase();
}
}
let key = keyFromKeyCode[keycode];
if (key) {
return key;
}

return (
(modifiers.shiftKey && keyFromKeyCodeWithShift[keycode]) ||
keyFromKeyCode[keycode]
);
}

/**
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/dom/trigger-key-event-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,12 @@ module('DOM Helper: triggerKeyEvent', function (hooks) {
await checkKey(90, 'z');
await checkKey(65, 'A', { shiftKey: true });
await checkKey(90, 'Z', { shiftKey: true });
await checkKey(49, '!', { shiftKey: true });
await checkKey(187, '+', { shiftKey: true });
await checkKey(38, 'ArrowUp', { shiftKey: true });

// an invalid keyCode
await checkKey(999, '');
});

test('The value of the `event.keyCode` is properly inferred from the given key', async function (assert) {
Expand Down