Hello,
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. Holding down a different key can send the signal through an alternate track, which is why it sometimes works.
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.
Hardware troubleshooting is out of the supporting scope of this forum. If if it is confirmed to be a hardware issue, it is best that you post in the HP community instead.