Force shut down the MacBook by holding down the Power key until it does shut down.
Next boot into Safe Mode according to How to use safe mode on your Mac and test to see if the problem persists. Reboot normally and test again.
NOTE 1: Safe Mode boot can take up to 3 - 5 minutes as it's doing the following;
• Verifies your startup disk and attempts to repair directory issues, if needed
• Loads only required kernel extensions (prevents 3rd party kernel/extensions from loading)
• Prevents Startup Items and Login Items from opening automatically
• Disables user-installed fonts
• Deletes font caches, kernel cache, and other system cache files
NOTE 2: if you have a wireless keyboard with rechargeable batteries connect it with its charging cable before booting into Safe Mode. This makes it act as a wired keyboard as will insure a successful boot into Safe Mode.
If the above failed to help reboot into the Recovery volume (boot with the Command + R keys held down - Intel Macs) or (How to Boot an M1 Mac into Recovery Mode) and run Disk Utility's First Aid on all volumes you can. Reboot normally. If that fails to help reboot into the Recovery volume and reinstall the system.