Aurelia 2 keydown.trigger preventing default without a way to disable it

In Aurelia 1, we could disable the preventDefault() call that the framework does internally when binding keydown.trigger by returning true from the event handler. In v2, this doesn’t seem to work, returning true from the event handler will not stop Aurelia 2 from still calling preventDefault() on the keyboard event. This stops the pressed key from printing in the input control.

What’s also interesting is that using keydown.delegate instead seems to not call preventDefault() and solves my use-case, but I’m trying to prefer using .trigger instead as that is the preferred binding mechanism from what I understand.

Is there a different way to do this now?

HTML:

<input type="text" keydown.trigger="handleKey($event)" />

JS:

handleKey(ev: KeyboardEvent) {
   return true;
}
3 Likes

With Aurelia2 you can always do that with ev.preventDefault().

2 Likes

as of 2.0.0 beta 11, this is still the issue.
the workaround that works as of now is to use keypress.capture

Did you try return true in the handler like the example above?

@tareqimbasher

Is there a different way to do this now?

Not yet, but we likely will disable the prevent default by default, and add an option to enable it for v1 compat, since with event modifier, it’s really simple to prevent default:

<button click.trigger:prevent="doThingsWithoutSubmit()">...</button>
1 Like

It’s resolved in this PR https://github.com/aurelia/aurelia/pull/1926

And the doc is at Migrating to Aurelia 2 | The Aurelia 2 Docs