Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | export class Key {
private code: string;
public isDown: boolean;
public isUp: boolean;
private press?: () => void;
private release?: () => void;
constructor(keyCode: string) {
this.code = keyCode;
this.isDown = false;
this.isUp = true;
window.addEventListener('keydown', this.downHandler.bind(this), false);
window.addEventListener('keyup', this.upHandler.bind(this), false);
}
private upHandler(event: KeyboardEvent): void {
if (this.isUp && isTyping()) return;
if (event.code === this.code) {
if (this.isDown && this.release) {
this.release();
}
this.isDown = false;
this.isUp = true;
}
// event.preventDefault();
}
private downHandler(event: KeyboardEvent): void {
if (isTyping()) return;
if (event.code === this.code) {
if (this.isUp && this.press) {
this.press();
}
this.isDown = true;
this.isUp = false;
}
// event.preventDefault();
}
public setPress(pressCall: () => void): void {
this.press = pressCall;
}
public setRelease(releaseCall: () => void): void {
this.release = releaseCall;
}
public reset(): void {
this.isDown = false;
this.isUp = true;
}
}
export function isTyping(): boolean {
if (document.activeElement?.tagName === 'INPUT') return true;
return false;
}
|