If your Mac is acting up and you suspect a hardware malfunction, try running Apple Diagnostics to see if it identifies any issues. Instructions here:
Have a Recent Apple MacBook or MacBook Pro? Here’s What You Need to Know about the Butterfly Keyboard
If you have a MacBook, MacBook Air from 2018, or MacBook Pro from 2016 on, here’s what you need to know about the trouble-prone butterfly keyboards, including how to get Apple to repair a broken one for free.