Skip to content

Instantly share code, notes, and snippets.

@mjanssen
Created November 1, 2021 11:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjanssen/ae839866ef13c0191c7dae5a71d82fd0 to your computer and use it in GitHub Desktop.
Save mjanssen/ae839866ef13c0191c7dae5a71d82fd0 to your computer and use it in GitHub Desktop.
import { useEffect, useState } from "react";
export default function Piano() {
const [key, setKey] = useState(null);
useEffect(() => {
function onKeyPress(e) {
e.preventDefault();
const key = e.key.toLowerCase();
setKey(key);
}
window.addEventListener('keyDown', onKeyPress);
return () => {
window.removeEventListener('keyDown', onKeyPress);
}
}, [])
return <Component keys={key} />
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment