cbernsdorf
cbernsdorf

Reputation: 83

Handle window.keyPress event in Aurelia app

I am wondering if anyone can provide any insight into how I would hook into the window.keyPress() event in an Aurelia app. I am looking to capture bar code scanner input and direct the scanned text into the appropriate input based on what the scanned value is.

I tried putting window.addEventListener("keypress", HandleKeyInput, false) in the activate() of my view model but this errors from the app-router with "HandleKeyInput is not defined" even though I have this function in my view model.

I am wondering what the correct approach for this scenario would be in regard to Aurelia.

Upvotes: 8

Views: 2328

Answers (1)

Jeremy Danyow
Jeremy Danyow

Reputation: 26406

Here's an example: https://gist.run?id=f7837c986c38adeac5a58b8007c28b2a

export class App {
  activate() {
    window.addEventListener('keypress', this.handleKeyInput, false);
  }

  deactivate() {
   window.removeEventListener('keypress', this.handleKeyInput);
  }

  handleKeyInput = (event) => {
    console.log(event);
  }
}

Some good reading:

Upvotes: 6

Related Questions