Skip to content

Commit

Permalink
Merge pull request #662 from fcollonval/fix/skip-prevented-event
Browse files Browse the repository at this point in the history
Skip processing prevented default key event
  • Loading branch information
brichet authored Dec 13, 2023
2 parents 744b258 + 58cecdc commit 5d6d48e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
9 changes: 7 additions & 2 deletions packages/commands/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -511,8 +511,13 @@ export class CommandRegistry {
* events will not invoke commands.
*/
processKeydownEvent(event: KeyboardEvent): void {
// Bail immediately if playing back keystrokes.
if (this._replaying || CommandRegistry.isModifierKeyPressed(event)) {
// Bail immediately if playing back keystrokes
// or if the event has been processed
if (
event.defaultPrevented ||
this._replaying ||
CommandRegistry.isModifierKeyPressed(event)
) {
return;
}

Expand Down
24 changes: 24 additions & 0 deletions packages/commands/tests/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,30 @@ describe('@lumino/commands', () => {
expect(count).to.equal(0);
});

it('should not dispatch on a prevented keydown event', () => {
let called = false;
registry.addCommand('test', {
execute: args => {
called = true;
}
});
registry.addKeyBinding({
keys: ['Ctrl ;'],
selector: `#${elem.id}`,
command: 'test'
});
const event = new KeyboardEvent('keydown', {
keyCode: 59,
ctrlKey: true,
cancelable: true
});

event.preventDefault();

elem.dispatchEvent(event);
expect(called).to.equal(false);
});

it('should dispatch with multiple chords in a key sequence', () => {
let count = 0;
registry.addCommand('test', {
Expand Down

0 comments on commit 5d6d48e

Please sign in to comment.