I have an Arduino Mega 2560 connected to a Windows 8 laptop. I want to program it to send keystrokes to the attached computer (when a pin's digital read goes from HIGH to LOW.)

The trouble is, I cannot find any way to program my Arduino Mega to send keystrokes.

The supposedly "core" Keyboard and Mouse libraries only support 32u4 boards, and other libraries that give the Arduino keystroke functionality are out of sight. When I googled the issue, I found no question or explanation of how to send keystrokes on the Arduino Mega. When I searched further, I thought the V-USB library would be a potential workaround, but the library throws numerous errors when imported into an Arduino sketch (even on the 1.0.0 IDE it was designed for.)

After hours of searching, two final workarounds seem to have popped up:

  1. Some method to run a Python code to send keystrokes to the PC, and then connect the Arduino code to Python. (I don't know Python...)
  2. Use some program called Gobetwino, which runs between the Arduino and PC, to convert Serial commands into keystrokes somehow. (Pressing and releasing a key can't be simulated very well by repeatedly sending Serial commands, I think.)

Both are very unideal, very tedious, and are equally likely to just not work. I have spent hours on this.

Does anyone have any suggestions to get me out of this hell? Libraries, workarounds, anything?

(Feel free to PM me questions for clarification.)

  • 1
    Both the Mega 2560 and the Uno R3 have the same second MCU, so I'm marking this one as a duplicate. – Ignacio Vazquez-Abrams Jul 17 '15 at 6:01
  • The V-USB library is the only practical way. You need a couple of resistors and zener diodes. I suggest you post the exact V-USB library you are attempting to use, and the exact error messages. – Nick Gammon Jul 17 '15 at 6:02
  • However reflashing the USB interface chip is not really an ideal solution. – Nick Gammon Jul 17 '15 at 6:03
  • 1
    Scrap the Mega2560 and use a Leonardo. – Majenko Jul 17 '15 at 11:02
  • Why do you want to send keystrokes? What do you want to achieve? Wouldn't it be easier to just signal the pin change via different means? – fuenfundachtzig Jul 17 '15 at 18:39

You are right, the mega can't do what you want unless something in the middle catches the serial/IO and translates to a keystroke command.

Another option, more expensive, is to buy an Arduino Leonardo which is also available as a small usb stick type of device. Then use serial/analog or ano from the mega to the Leonardo.

The Leonardo boards can emulate any usb device such as keyboard, mouse, joystick etc.

Maybe not of interested but also look at Teensy 3.1. It can do what the Leonardo does but is 32 bit and very flexible. The only benefit of the mega is the massive number of pins. Teensy costs about $20.

It all depends on what your project requires.

  • Untrue. The Mega2560 hardware is capable of both USB HID (see comments) and PS/2 keyboard interfaces. – Chris Stratton Jul 17 '15 at 13:11
  • Not sure if that is true. It is the ATmega8U2 usb on the same board as the mega that could do this but requires some difficult non-arduino re-programming? The Leonardo is programmed via the sketch as normal so it's a simple solution designed for this purpose. Please correct me if I am wrong about this? – Visual Micro Jul 17 '15 at 13:22

Not the answer you're looking for? Browse other questions tagged or ask your own question.