Adding a keyboard event to plotlyjs

I want to listen to keyboard event in plotlyjs(spacebar key pressed to be more specific). When spacebar is pressed I want to discontinue line drawing on mouse click.

I have added eventListener to listen to key events but does not seem to work. Is there any specific way to capture keyboard events.

plotCon.addEventListener('click', function(e){
      var clickCor = e.target.getBoundingClientRect();
      data[0].x.push(plotCon._fullLayout.xaxis.p2d(e.clientX - clickCor.left));
      data[0].y.push(plotCon._fullLayout.yaxis.p2d(e.clientY - clickCor.top));
      image1Cor.concat(data[0].x, data[0].y);    
      Plotly.update(plotCon, data, layout, config, {displayModeBar: false});
    });

    plotCon.addEventListener('keydown', function(e) {
      if(e.key == " " || e.code == "Space" || e.keyCode == 32) {
        alert('key pressed');
      }
    });