keypress

Estimated reading time: 1 minute
  • https://forum.vuejs.org/t/capture-keypress-for-all-keys/14560/2
 mounted() {
    window.addEventListener("keypress", function(e) {
      console.log(String.fromCharCode(e.keyCode));
    }.bind(this));
  }

Solution B: a closure

 mounted() {
    const self = this
    window.addEventListener("keypress", function(e) {
      // use self instead of this in here
      console.log(String.fromCharCode(e.keyCode));
    });
  }

Solution C: Arrow function

 mounted() {
    window.addEventListener("keypress", e => {
      console.log(String.fromCharCode(e.keyCode));
    });
  }

This is not a Vue-specific problem - understanding how this works in Javascript is fundamental, you should read up on it.

vue, vusjs