If a simple reboot of your Mac doesn’t fix things, see if the problem still happens in safe mode. Safe mode will often correct weird software behavior by forcing the OS to clear caches, do disk repair and other self-maintenance and housekeeping and temporarily disables third-party mods.
How to use safe mode on your Mac - Apple Support
TIP - Use a wired USB keyboard for this. If you have the Magic Keyboard, plug that into the Mac with its charging cable.
Use your Mac in safe mode for a little while, say 30 minutes, and then exit safe mode by restarting your Mac normally and reevaluate the problem.
If the problem goes away in safe mode but returns after restarting normally, that may indicate a conflict with some installed software. To help diagnose that, download and run the free version of EtreCheckPro EtreCheck Pro.
Then post back here with your report as instructed here: How to use the Add Text Feature When Post… - Apple Community