How to keep a square shape from blinking when spacebar is pressed

int iArray[]=new int[1000];
int iIndex=0,iMax=25,iBeat=0;

//******************************************************************************
void setup()
//******************************************************************************
{
  size(1000,600);
  for(int iX=0;iX<975;iX++)iArray[iX]=300;  //preload array with baseline
                                            
  fill(0,200,0);
  noStroke();
}
//******************************************************************************
void draw()
//******************************************************************************
{
  background(100);
  for(iIndex=25;iIndex<iMax;iIndex++)
  {
    ellipse(iIndex,iArray[iIndex],3,3);    //output dynamically increasing
    
  } 
  
     if(iBeat==1)
     {                                     
      rect(iIndex,200,20,20);
     }
     
     iBeat=0;
  
 
  iMax+=2;         //array output increases by 2 elements each draw() loop
  

  delay(10);      
  
  if(iMax>975)                   //when output array size reaches >800
  {                              //...then reset array size back to 25
    iMax=25;
    for(int iX=0;iX<975;iX++)iArray[iX]=300;
  }
}
//******************************************************************************
void keyPressed()
//******************************************************************************
{
  if(key==0x20)
{
   iBeat=1;
 }
                                      
}
//******************************************************************************

I’m trying to put a green square shape above the green line, while the green line is moving horizontally like a flat line when the spacebar is pressed, However, when I press the spacebar, the square shape does appear but goes away after I’m done pressing the spacebar

In your draw loop you are setting your key integer after you draw your rect everytime draw is called.

So unless space bar is pressed the rectangle will not be drawn. That’s the behaviour you have coded. If it’s not what you want then you need to add more logic to allow the rect to be drawn.