Arduino Leonardo as USB KEYBOARD incompatibility with KVM device

My intention is to set up the Arduino Leonardo as a USB keyboard so that by pressing a single button I can simulate a certain Keyboard Key press to a KVM device. I’m trying to simulate an Up Arrow Key press.

My code is below, and works perfectly fine and as expected when the Arduino is connected to a Windows computer but when I connect the Arduino to the KVM device via USB the Up Arrow Key is not simulated.

When I connect any other purpose built USB Keyboard to the KVM it works just fine Hence I have come to the realisation that it could quite possibly be a compatibility issue between the Arduino when used as a USB Keyboard and the KVM.

I think I need to edit something in one of the Arduino libraries and maybe even in my code but I am a total novice and am totally lost. Can anybody help please?? MUCH APPRECIATED!!!

#include <Keyboard.h>

int buttonPin = 2;

void setup()
pinMode(buttonPin, INPUT);
digitalWrite(buttonPin, HIGH);

void loop()
if (digitalRead(buttonPin) == 0)

I guess there might be a problem that the Leonardo at startup is a serial device first (to enable uploads of new programs). The KVM device (it might help to know which KVM device we're talking about) might simply ignore the device as it's not purely a keyboard device. Even when your sketch is activated there is also a virtual serial interface active to again enable the upload functionality by listening for a 1200 baud connection in which case the Leonardo will reboot. If these other USB devices disturb your KVM device, you must change your code to disable all serial functionality. The IDE developers made a lot of effort to prevent this so it might be tricky to realize inside the IDE.

Thanks for your reply pylon.

The KVM im using is an Aten CS1308, I made sure I have the latest firmware as they have released many updates to fix keyboard and mouse compatilibity issues.

Sadly I dont understand about the technical terms you are using, ie listening for a 1200 Baud.

do you have a straightforward advise for me that Me as a novice can understand and implement?

Almost all KVM switched have some sort of hot-key command mode (or pass though setting) which may interfere with what you are trying to do (check the manual). Also, you may want to enable/disable USB legacy mode in the BIOS of the computer which does not comply because that may also be the culprit.

And as #1 said, if the Leonardo is not registred as a keyboard, the KVM switch may simply ignore it as a bogus device.

do you have a straightforward advise for me that Me as a novice can understand and implement?

Excuse me for providing a to detailed explanation.

I try give a more end user friendly description of what happens. A device you connect to the USB port of a computer may just show as a single device in your operating system (standard keyboard or mouse) but it may also have several virtual devices inside that all are operated of the same USB connection (keyboard with integrated trackpad, multi-functional printer that also shows up as a scanner, etc.). The Leonardo always activates a virtual serial device when it's connected to a computer. During startup this is directly available for uploads for a few seconds, after that the programmed sketch is started. Once the sketch is started not only that sketch runs but the IDE automatically adds code to the uploaded program to have a virtual serial device running in the background, even if you do not use the Serial object to send or receive data from the controlling computer. That device is only there to listen for a connection. If that connection is at 1200 baud and the connection is immediately closed (without sending any data), that's the signal for the Leonardo that the IDE wants to upload a new sketch and it has to automatically reset. If that functionality is not included you could upload one sketch over the USB connection and never use it again for sketch uploading. This is not what most users would expect so the Arduino people implemented it the way that most users feel happy.

In your case I guess that the KVM switch has a very simple USB host interface. If a USB device is connected that does not only identify itself as a keyboard, it simply ignores it and doesn't react on any message received from that device. That way the KVM device doesn't have to implement the complete USB software stack but can keep that part small and rather simple (I guess a connected USB hub would also be ignored).

You can program the Leonardo to simply identify itself as a keyboard (and not serial stuff) but you cannot do that with the IDE (to my knowledge). And you cannot upload the so built program by USB but you have to use an ICSP programmer to directly write the processor's flash. Unfortunately this is out of a novice user's reach and I have no better solution for that.

Thank you very much for your in depth explanation pylon. I now totally understand how and why the Adruino works the way it does.

I do also feel that the KVM I have has a very very simple USB host interface.

Seen as using an ICSP programmer is far from reach for me I have a different approach in mind.

If I salvage a purpose built keyboard and have this keyboard connected to the KVM, then use one of the output pins from my Arduino to control a transistor to make the physical connection for the UP Arrow Key on the keyboard. This way the KVM will receive a real key press from a real keyboard.

Do you think this method will work?

It is possible to make an Arduino look more like a real USB keyboard using the IDE. This is useful for Assistive Tech or a keyboard extender where sometimes old computers running OSes such as XP are confused by the Arduino Pluggable USB descriptors and the extraneous USB serial port.

Some AVR Core files must be modified. After these changes are made, the Leonardo no longer appears with a USB serial port so uploading new firmware from the IDE is no longer automatic. But the extra step is easy.

Double click on the Leonardo RESET button to force the Leonardo into bootloader mode. The USB serial port should appear. Press the Upload button in the IDE to upload new firmware. Modifying the AVR Core files does not change the bootloader so this should always work.

This project has modified AVR Core files. It was supposed to be part of an USB extender and/or IP KVM but I am not working on it any more.

I found copying the AVR Core files into the same directory as the sketch then modifying them works. The IDE seems to override the files in the AVR board package directory if it finds them in the sketch directory. This is Good because if the Core files are modified in the package directory, the next update to the AVR board package will overwrite the changes!

Thanks for your reply gdsports, very informative and helpful. I will have a look at the link you included to try get an understanding of what has been modified and if I can follow the same/similar procedures.

Thanks again!