Try and use another keyboard instead to see if this is a problem with the keyboard.
If it is not a problem with the keyboard, first reboot your Mac to see if the issue persists.
If it persists, boot in the safe mode to see if the issue persists
- On your Mac, choose Apple menu > Shut Down.
- Wait for your Mac to shut down completely. A Mac is completely shut down when the screen is black and any lights (including in the Touch Bar) are off.
- Press and hold the power button on your Mac until “Loading startup options” appears.
- Select a volume.
- Press and hold the Shift key, then click Continue in Safe Mode.
- The computer restarts automatically. When the login window appears, you should see “Safe Boot” in the menu bar.
IF the issue is gone in the safe mode, it is caused by an external piece of software, not to something within macOS.
If the issue persists in safe mode, Contact - Official Apple Support for further assistacne and to set up service for your device