Handle window.keyPress event in Aurelia app

I am wondering if anyone can give an idea of ​​how I could connect to an event window.keyPress()in an Aurelia app. I want to capture the input of a barcode scanner and direct the scanned text to the appropriate input, based on what the scanned value is.

I tried to put window.addEventListener("keypress", HandleKeyInput, false)in activate()my view model, but these errors from the router application with "HandleKeyInput not defined", although I have this function in my view model.

I am wondering what is the right approach for this scenario to be relevant to Aurelia.

+4
source share
1 answer

: 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);
  }
}

:

+3

Source: https://habr.com/ru/post/1607849/


All Articles