Sounds like something's stealing focus from your active window. Most common culprit if you have any Adobe apps installed is the Creative Cloud helper — it'll pop a "something needs your attention" dialog without bringing itself to the front, so you have to click your window back to keep typing. Open Activity Monitor and sort by CPU at the moment it happens, that usually outs whoever's doing it.
If you're on a wireless keyboard, a dying battery can also cause brief BT dropouts the OS treats as focus events. Worth ruling out before digging deeper.