Hello, this is Emily.
This is usually a hardware issue with the keyboard. Underneath the keyboard, there is a circuit board with numerous signal tracks. Cracks in the tracks, dust or debris buildup, or corrosion can disrupt the signal flow. Since spamming seems to help, it is likely crack.
To diagnose a keyboard problem, it's essential to test with the onscreen keyboard or a different keyboard via USB or Bluetooth. If the alternative keyboard works, it's certain that the issue is hardware-related.