React Piano inspired by FCC Drum Machine project

Hi All,

I am messing around with creating a piano with React based off the drum machine project.

Here is a link (there are no sounds connected yet, just key events and animation)

The issue I have is on a keydown event. When I hold down a key on the keyboard, the associated piano key just continually retriggers over and over until I let go of the keyboard key. I am trying to implement it that so when I hold down the key, it acts like a real piano, and won’t retrigger indefinitely.

Does anyone have any experience or ideas regarding this? I am having a hard time trying to figure it out.

  componentDidMount() {
    document.addEventListener('keydown', this.handleKeyPress);
  }
  componentWillUnmount() {
    document.removeEventListener('keydown', this.handleKeyPress);
  }
  handleKeyPress(e) {
    console.log('outside of if with keyboard keycode as ' + e.keyCode + ' vs ' + this.props.keyCode)
    if(e.keyCode == this.props.keyCode){
      console.log('IF TRIGGERED')
      this.animate();
      setTimeout(() => this.animate(), 100);
    }
  }

After much trial and error, it works, and with sound!

https://codepen.io/stoosh/pen/ejKoRM

now to add midi support…

Yea that will definitely get added in the near future. I want to first PoC midi access with react. It’s a bit tricky so far. Seems like I will have to use a special midi js library on top of the midiaccess api.

Added midi, more keyboard, more keys, and clicks! (midi only works in chrome)

Although, running into some issues with the clicking. It only lets you click on the keys with characters on them, and only if you click on where the character is located.
Feedback appreciated!

https://codepen.io/stoosh/pen/VBBPMX?editors=0111