I personally don’t put a whole lot of effort into cleaning keyboards. You can carefully take it apart and clean out every little bit of it with a Q-tip, but I’ve done that before and it’s really, really, tedious.
What I usually do is run the keyboard through the dishwasher. There are a few tricks to doing this.
Place the keyboard upside down so that the keys face down.
Wrap up the cord so that it can’t get caught in the dishwasher’s moving parts.
Don’t use soap. Some are ok, but others will leave a filmy residue that may actually make your keyboard worse.
Turn off the heat cycle at the end or you’ll bake your keyboard to death. Be careful. Sometimes this is labeled as something like “power saver dry” and you have to turn it ON to turn the heat OFF.
Let the keyboard dry out for a couple of days before using it.
Note that wireless keyboards and keyboards with those fancy little LCD displays in them tend not to survive this procedure very well. Otherwise, I’ve personally had an excellent success rate with this.
There is a chance that it’s not just dirty contacts making your keyboard go wonky (it could be a physical hardware problem like an intermittently bad electrical connection), so I personally would give it one run through the dishwasher, and if that doesn’t fix it, I’d go buy a new keyboard.