capture keys on both keypress and keydown

jh-changes
Shin'ya Ueoka 7 years ago
parent 5ac1f60ece
commit 4b5616b524
  1. 25
      src/components/content-input.js

@ -2,12 +2,31 @@ import messages from '../content/messages';
export default class ContentInputComponent { export default class ContentInputComponent {
constructor(target) { constructor(target) {
this.pressed = {};
target.addEventListener('keypress', this.onKeyPress.bind(this)); target.addEventListener('keypress', this.onKeyPress.bind(this));
target.addEventListener('keydown', this.onKeyDown.bind(this)); target.addEventListener('keydown', this.onKeyDown.bind(this));
target.addEventListener('keyup', this.onKeyUp.bind(this)); target.addEventListener('keyup', this.onKeyUp.bind(this));
} }
onKeyPress(e) { onKeyPress(e) {
this.capture(e);
}
onKeyDown(e) {
this.capture(e);
}
onKeyUp(e) {
this.pressed[e.key] = false;
}
capture(e) {
if (this.pressed[e.key]) {
return;
}
this.pressed[e.key] = true;
if (e.target instanceof HTMLInputElement || if (e.target instanceof HTMLInputElement ||
e.target instanceof HTMLTextAreaElement || e.target instanceof HTMLTextAreaElement ||
e.target instanceof HTMLSelectElement) { e.target instanceof HTMLSelectElement) {
@ -22,10 +41,4 @@ export default class ContentInputComponent {
ctrl: e.ctrlKey ctrl: e.ctrlKey
}); });
} }
onKeyDown() {
}
onKeyUp() {
}
} }