KVM switches are great to connect your periperials to different computers on a click of a button. 

Do not use the dediacted USB keyboard port

If your KVM switch has a dedicated USB port for a keyboard: 

Do not plug in your Magic Keyboard there, otherwise unforseen things may happen.

On the console section use a generic USB port to connect your Magic Keyboard.

Tripp Liate: 2-Port DisplayPort KVM Switch
Example ports of a Tripp Lite: 2-Port DisplayPort KVM Switch

The issue is not within the KVM switch. But the Magic Keyboard may send USB HID data which is not what the KVM switch expects and described in the HID descriptor.