This is an operating system problem. When a new device is plugged in and claims to be a keyboard, it should lock the screen and not be accepted as input until it has typed the user's login password.
Or have the OS block it initially and popup a message saying what the device claims to be. The user either accepts or rejects, and if rejected the block stays in place. Only exception would be for the first keyboard/mouse plugged (unless there is a built in one, in which case even this wouldn't be an exception).
Different protocol for startup, where you select the keyboard you want. For a desktop with a good keyboard and a malicious USB both plugged in, it is going to need some way to know which one to go with anyways. The real threat is someone plugging in the bad USB after they are logged into their machine, at which point you already have a good keyboard (or are using mouse + onscreen keyboard).
Maybe even don't go with any keyboards to start, only a mouse and an onscreen keyboard and all others have to be approved.
I suppose you'd need a user interface for authorizing things like that. Ideally, it'd be on the lock screen; show a message which says "New keyboard detected; sign in with that keyboard or click here and sign in with your existing keyboard to enable it". It's extra work, and makes it cross-cut through more modules, but it's hardly insurmountable.
I wonder whether it's smart enough to adapt its keystrokes for non-Windows platforms, or whether it just does random things to peoples' computers in that case?