I've never understood why operating systems allow focus switching.
It's always been a pox on Windows, but macOS does this more and more these days. Just this morning, I plugged in a USB hard drive, then returned to my other work while it mounted. Suddenly, I'm typing a memo into Finder's password field to unlock the drive. Just. Stop. It.
If you need my attention, there is no shortage of methods to do that — Beep. Bounce the icon in the dock. Notifications messages. Your stupid program is not more important than whatever I'm doing at that moment in time.
KDE calls this "Focus stealing" and has a setting to disable it.
Normally I would lol at your misfortune but I actually got bitten by this too.
I restarted after a macOS update (remember when they used to update in the background and now every one requires a restart like Windows?) and 1Password needed my master password to unlock in the browser but apparently Messages app took a few seconds to load and when I saw the notification to enter my 1Pass master password I started typing and at that moment Messages took focus and I typed my master password into a message and hit enter to send.
Thankfully it was to my girlfriend, but still wtf.