export interface Controls { up: boolean, right: boolean, down: boolean, left: boolean, space: boolean, } export let current = { up: false, right: false, down: false, left: false, space: false, } 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 === ' ') return { ...current, space: isDown } else return current }