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

Key fix #41

Merged
merged 9 commits into from
Mar 30, 2021
Merged
Show file tree
Hide file tree
Changes from 8 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
5 changes: 3 additions & 2 deletions src/hotkey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
//
// Returns key character String or null.
export default function hotkey(event: KeyboardEvent): string {
const elideShift = event.code.startsWith('Key') && event.shiftKey
return `${event.ctrlKey ? 'Control+' : ''}${event.altKey ? 'Alt+' : ''}${event.metaKey ? 'Meta+' : ''}${
event.shiftKey && event.key.toUpperCase() !== event.key ? 'Shift+' : ''
}${event.key}`
event.shiftKey && !elideShift ? 'Shift+' : ''
}${elideShift ? event.key.toUpperCase() : event.key}`
}
42 changes: 39 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ describe('hotkey', function () {
it("doesn't trigger elements whose hotkey has been removed", function () {
setHTML('<button id="button1" data-hotkey="b">Button 1</button>')
uninstall(document.querySelector('#button1'))
document.dispatchEvent(new KeyboardEvent('keydown', {key: 'b'}))
document.dispatchEvent(new KeyboardEvent('keydown', {code: 'KeyB', key: 'b'}))
assert.deepEqual(elementsActivated, [])
})

it('triggers elements with capitalised key', function () {
setHTML('<button id="button1" data-hotkey="B">Button 1</button>')
document.dispatchEvent(new KeyboardEvent('keydown', {shiftKey: true, key: 'B'}))
document.dispatchEvent(new KeyboardEvent('keydown', {shiftKey: true, code: 'KeyB', key: 'B'}))
assert.include(elementsActivated, 'button1')
})
})
Expand All @@ -103,7 +103,34 @@ describe('hotkey', function () {
document.body.removeEventListener('keydown', handler)
done()
})
document.body.dispatchEvent(new KeyboardEvent('keydown', {key: 'J'}))
document.body.dispatchEvent(new KeyboardEvent('keydown', {shiftKey: true, code: 'KeyJ', key: 'J'}))
})

it('keydown with shift and lowercase letter', function (done) {
document.body.addEventListener('keydown', function handler(event) {
assert.equal(eventToHotkeyString(event), 'Control+J')
document.body.removeEventListener('keydown', handler)
done()
})
document.body.dispatchEvent(new KeyboardEvent('keydown', {ctrlKey: true, shiftKey: true, code: 'KeyJ', key: 'j'}))
})

it('keydown with shift and uppercase letter', function (done) {
document.body.addEventListener('keydown', function handler(event) {
assert.equal(eventToHotkeyString(event), 'Control+J')
document.body.removeEventListener('keydown', handler)
done()
})
document.body.dispatchEvent(new KeyboardEvent('keydown', {ctrlKey: true, shiftKey: true, code: 'KeyJ', key: 'J'}))
})

it('keydown with lowercase letter', function (done) {
document.body.addEventListener('keydown', function handler(event) {
assert.equal(eventToHotkeyString(event), 'Control+j')
document.body.removeEventListener('keydown', handler)
done()
})
document.body.dispatchEvent(new KeyboardEvent('keydown', {ctrlKey: true, code: 'KeyJ', key: 'j'}))
})

it('keydown with number', function (done) {
Expand All @@ -114,6 +141,15 @@ describe('hotkey', function () {
})
document.body.dispatchEvent(new KeyboardEvent('keydown', {key: '1'}))
})

it('keydown with symbol', function (done) {
document.body.addEventListener('keydown', function handler(event) {
assert.equal(eventToHotkeyString(event), 'Control+Shift+`')
document.body.removeEventListener('keydown', handler)
done()
})
document.body.dispatchEvent(new KeyboardEvent('keydown', {ctrlKey: true, shiftKey: true, key: '`'}))
})
})

describe('hotkey sequence support', function () {
Expand Down