export interface Controls { up: boolean, right: boolean, down: boolean, left: boolean, spaceCount: number, } export let current = { up: false, right: false, down: false, left: false, spaceCount: 0, } document.addEventListener('keydown', event => { current = update(current, event.key, true) }) document.addEventListener('keyup', event => { current = update(current, event.key, false) }) function update(current: Controls, key: string, isDown: boolean): Controls { if (key === 'ArrowUp') return { ...current, up: isDown } else if (key === 'ArrowRight') return { ...current, right: isDown } else if (key === 'ArrowDown') return { ...current, down: isDown } else if (key === 'ArrowLeft') return { ...current, left: isDown } else if (key === ' ' && !isDown) return { ...current, spaceCount: current.spaceCount + 1 } else return current }