Key inputs are too slow(using Arduino Uno as HID device)

I am using my Arduino Uno as a HID device(keyboard). But the key inputs are too slow. I mean, 1)there is a short delay from my button press to the actual input. 2)When I keep the button pressed(corresponding to 'e'), normally it should be like 'eeeeeeeeeeeeeeeeeee...'(almost no delay between 'e's), but the actual input is 2~300ms delay between each 'e's.

These two problems didn't occur when I only had one button. But when I connected 3 buttons, those problems occur.

My code:

#define KEY_Q 20 //blue button
#define KEY_S 22 //green button
#define KEY_E 8 //red button

uint8_t buf[8] = {0 };

  void releaseKey() {
  buf[0] = 0;
  buf[2] = 0;
  Serial.write(buf, 8); // Release key  
  delay(200);
  }
  
void setup() {
  Serial.begin(9600);
  delay(300);
  }
  
 void loop(){
  if(digitalRead(2)==1){
     buf[2] = KEY_E;
      Serial.write(buf, 8);
    }else{
      releaseKey();
    }

   if(digitalRead(3)==1){
    buf[2] = KEY_S;
    Serial.write(buf, 8);
   }else{
    releaseKey();
   }

   if(digitalRead(4)==1){
    buf[2] = KEY_Q;
    Serial.write(buf, 8);
   }else{
    releaseKey();
   }
 }

You did not define the input modes. Also you should give names to the Arduino pins so you know which one is which. eg: pinMode(pinBlueButton, INPUT_PULLUP);

How are your buttons wired?

Have you heard of debouncing? You might find that at least the first press on the button will also show a release a few milliseconds later, triggering your 200ms delay.

When there are no buttons pressed your sketch will be continuously calling releaseKey() and since it calls delay(200) your program will effectively stop for 200ms. As a result you are always going to see a time lag of on average 100ms when a button is first pressed.

I am not sure why you see delays between characters appearing when a key is held down. The function digitalRead does not say it debounces so presumably it does not. However the delay caused by releaseKey should remove bounce almost all the time.

All Arduino boards have at least one serial port (also known as a UART or USART): Serial. It communicates on digital pins 0 (RX) and 1 (TX) as well as with the computer via USB. Thus, if you use these functions, you cannot also use pins 0 and 1 for digital input or output.

Is your output going to USB or the digital pins? Is anything wired to the pins?

ardly: Is your output going to USB or the digital pins? Is anything wired to the pins?

The serial output goes to the 16u2, as usual, but the 16u2 has kludged firmware allowing the data to be forwarded to the USB connection as HID reports.