Well, it could be dirt, dust, liquid even inside the keyboard, so I would check for that first. Always good to clean it out.
If there is nothing physically in it, but it continues to do this then it could be the connections within the keyboard. In that case, it may well just need replacing (the keyboard). Or you could continue to just use an external one.