To defend against devices pretending to be a keyboard to type malicious code, all you need is to display a random code on the screen and ask the user to enter it.
Any legitimate input device will easily pass this check, while devices that pretend to be keyboards won't because they have no view of the screen.
Any legitimate input device will easily pass this check, while devices that pretend to be keyboards won't because they have no view of the screen.