To use the piano, import the Piano component, KeyboardShortcuts utility, MidiNumbers utility, and the required CSS file.
Note: Importing CSS requires a CSS loader (like in create-react-app). If you don't have one, manually copy src/styles.css from the package into your project.
import { Piano, KeyboardShortcuts, MidiNumbers } from 'react-piano';
import 'react-piano/dist/styles.css';
function App() {
const firstNote = MidiNumbers.fromNote('c3');
const lastNote = MidiNumbers.fromNote('f5');
const keyboardShortcuts = KeyboardShortcuts.create({
firstNote: firstNote,
lastNote: lastNote,
keyboardConfig: KeyboardShortcuts.HOME_ROW,
});
return (
<Piano
noteRange={{ first: firstNote, last: lastNote }}
playNote={(midiNumber) => {
// Implement your audio playback logic here
}}
stopNote={(midiNumber) => {
// Implement your audio playback logic here
}}
width={1000}
keyboardShortcuts={keyboardShortcuts}
/>
);
}