Resetting the SMC will resolve software-based issues with the keyboard. If the issue persists then you will have to reinstall macOS or run a hardware diagnostic.
Look at https://support.apple.com/en-us/HT201295 to reset the SMC for your Mac. You can contact me if you need help with reinstalling macOS or running a hardware diagnostic.
Ultimately, if none of the software solutions work then you will have to have your Mac serviced..