Not pretty, however...
... gbhall gave you very sound advice.
As your problem is intermittent, IMO, it's unlikely that software is causing the problem. So, Yes: Bang, smack, whack the keyboard a couple/few times while holding it upside down and at varying angles.
You did not specify which you're using but, of course, if its a desktop's keyboard, you can use more force safely than with a laptop's. If it's a laptop, in order to minimize the "trauma" to the whole machine, you may wish to go to the trouble of removing the keyboard entirely then, (gently) bang, smack, whack it on the plastic-film-covered rear.
When my laptop or desktop keyboard acts as does yours, I follow up the above with a very liberal spray of alcohol to the face of the keyboard and then immediately lay it, upside down, on a paper towel for a minute-or-so. This "unglues" that long ago drop of sugary coffee I splashed!