fix infiltration keybinding

This commit is contained in:
Olivier Gagnon 2021-09-24 22:15:19 -04:00
parent 76e6cb4ecc
commit 65cb519801
10 changed files with 21 additions and 21 deletions

@ -33,7 +33,7 @@ export function BackwardGame(props: IMinigameProps): React.ReactElement {
const [answer] = useState(makeAnswer(difficulty)); const [answer] = useState(makeAnswer(difficulty));
const [guess, setGuess] = useState(""); const [guess, setGuess] = useState("");
function press(event: React.KeyboardEvent<HTMLElement>): void { function press(this: Document, event: KeyboardEvent): void {
event.preventDefault(); event.preventDefault();
if (event.keyCode === 16) return; if (event.keyCode === 16) return;
const nextGuess = guess + event.key.toUpperCase(); const nextGuess = guess + event.key.toUpperCase();

@ -36,7 +36,7 @@ function generateLeftSide(difficulty: Difficulty): string {
return str; return str;
} }
function getChar(event: React.KeyboardEvent<HTMLElement>): string { function getChar(event: KeyboardEvent): string {
if (event.keyCode == 48 && event.shiftKey) return ")"; if (event.keyCode == 48 && event.shiftKey) return ")";
if (event.keyCode == 221 && !event.shiftKey) return "]"; if (event.keyCode == 221 && !event.shiftKey) return "]";
if (event.keyCode == 221 && event.shiftKey) return "}"; if (event.keyCode == 221 && event.shiftKey) return "}";
@ -60,7 +60,7 @@ export function BracketGame(props: IMinigameProps): React.ReactElement {
const [right, setRight] = useState(""); const [right, setRight] = useState("");
const [left] = useState(generateLeftSide(difficulty)); const [left] = useState(generateLeftSide(difficulty));
function press(event: React.KeyboardEvent<HTMLElement>): void { function press(this: Document, event: KeyboardEvent): void {
event.preventDefault(); event.preventDefault();
const char = getChar(event); const char = getChar(event);
if (!char) return; if (!char) return;

@ -30,7 +30,7 @@ export function BribeGame(props: IMinigameProps): React.ReactElement {
const [choices] = useState(makeChoices(difficulty)); const [choices] = useState(makeChoices(difficulty));
const [index, setIndex] = useState(0); const [index, setIndex] = useState(0);
function press(event: React.KeyboardEvent<HTMLElement>): void { function press(this: Document, event: KeyboardEvent): void {
event.preventDefault(); event.preventDefault();
const k = event.keyCode; const k = event.keyCode;
if (k === 32) { if (k === 32) {

@ -32,7 +32,7 @@ export function CheatCodeGame(props: IMinigameProps): React.ReactElement {
const [code] = useState(generateCode(difficulty)); const [code] = useState(generateCode(difficulty));
const [index, setIndex] = useState(0); const [index, setIndex] = useState(0);
function press(event: React.KeyboardEvent<HTMLElement>): void { function press(this: Document, event: KeyboardEvent): void {
event.preventDefault(); event.preventDefault();
if (code[index] !== getArrow(event)) { if (code[index] !== getArrow(event)) {
props.onFailure(); props.onFailure();

@ -35,7 +35,7 @@ export function Cyberpunk2077Game(props: IMinigameProps): React.ReactElement {
const [index, setIndex] = useState(0); const [index, setIndex] = useState(0);
const [pos, setPos] = useState([0, 0]); const [pos, setPos] = useState([0, 0]);
function press(event: React.KeyboardEvent<HTMLElement>): void { function press(this: Document, event: KeyboardEvent): void {
event.preventDefault(); event.preventDefault();
const move = [0, 0]; const move = [0, 0];
const arrow = getArrow(event); const arrow = getArrow(event);

@ -1,22 +1,22 @@
import React, { useEffect } from "react"; import React, { useEffect } from "react";
interface IProps { interface IProps {
onKeyDown: (event: React.KeyboardEvent<HTMLElement>) => void; onKeyDown: (this: Document, event: KeyboardEvent) => void;
onFailure: (options?: { automated: boolean }) => void; onFailure: (options?: { automated: boolean }) => void;
} }
export function KeyHandler(props: IProps): React.ReactElement { export function KeyHandler(props: IProps): React.ReactElement {
let elem: any; useEffect(() => {
useEffect(() => elem.focus()); console.log("binding");
function press(this: Document, event: KeyboardEvent): void {
function onKeyDown(event: React.KeyboardEvent<HTMLElement>): void { console.log("press!");
if (!event.isTrusted) { const f = props.onKeyDown.bind(this);
props.onFailure({ automated: true }); f(event);
return;
} }
props.onKeyDown(event); document.addEventListener("keydown", press);
} return () => document.removeEventListener("keydown", press);
});
// invisible autofocused element that eats all the keypress for the minigames. // invisible autofocused element that eats all the keypress for the minigames.
return <div tabIndex={1} ref={(c) => (elem = c)} onKeyDown={onKeyDown} />; return <></>;
} }

@ -35,7 +35,7 @@ export function MinesweeperGame(props: IMinigameProps): React.ReactElement {
const [pos, setPos] = useState([0, 0]); const [pos, setPos] = useState([0, 0]);
const [memoryPhase, setMemoryPhase] = useState(true); const [memoryPhase, setMemoryPhase] = useState(true);
function press(event: React.KeyboardEvent<HTMLElement>): void { function press(this: Document, event: KeyboardEvent): void {
event.preventDefault(); event.preventDefault();
if (memoryPhase) return; if (memoryPhase) return;
const move = [0, 0]; const move = [0, 0];

@ -27,7 +27,7 @@ export function SlashGame(props: IMinigameProps): React.ReactElement {
interpolate(difficulties, props.difficulty, difficulty); interpolate(difficulties, props.difficulty, difficulty);
const [guarding, setGuarding] = useState(true); const [guarding, setGuarding] = useState(true);
function press(event: React.KeyboardEvent<HTMLElement>): void { function press(this: Document, event: KeyboardEvent): void {
event.preventDefault(); event.preventDefault();
if (event.keyCode !== 32) return; if (event.keyCode !== 32) return;
if (guarding) { if (guarding) {

@ -60,7 +60,7 @@ export function WireCuttingGame(props: IMinigameProps): React.ReactElement {
const [cutWires, setCutWires] = useState(new Array(wires.length).fill(false)); const [cutWires, setCutWires] = useState(new Array(wires.length).fill(false));
const [questions] = useState(generateQuestion(wires, difficulty)); const [questions] = useState(generateQuestion(wires, difficulty));
function press(event: React.KeyboardEvent<HTMLElement>): void { function press(this: Document, event: KeyboardEvent): void {
event.preventDefault(); event.preventDefault();
const wireNum = parseInt(event.key); const wireNum = parseInt(event.key);

@ -4,7 +4,7 @@ export function random(min: number, max: number): number {
return Math.random() * (max - min) + min; return Math.random() * (max - min) + min;
} }
export function getArrow(event: React.KeyboardEvent<HTMLElement>): string { export function getArrow(event: KeyboardEvent): string {
switch (event.keyCode) { switch (event.keyCode) {
case 38: case 38:
case 87: case 87: