como-emular-un-teclado-o-un-raton-con-arduino

How to emulate a keyboard or mouse with Arduino

  • 6 min

A little-known feature of certain microprocessors is their ability to emulate a keyboard or mouse when connected to a computer or other compatible device via USB.

This HID (Human Interface Device) emulation functionality is available natively on boards that incorporate ATMEGA 32u4 (Micro, Leonardo) or SAMD (Zero, Due, MKR) processors.

Other processors outside the Arduino “ecosystem” can also incorporate HID emulation. There are even certain ‘workarounds’ for processors like the Atmega 328 to act as HID, but generally, they don’t work very well.

What can this be useful for? For example, to interact with machines or programs to which we don’t have access to install software. For instance, we can control an Android video player, or even industrial machines, where we can’t install software but they are controlled by keyboard or mouse.

Another common use is to combine HID functionality with a sensor to make our “own” HID controllers. For example, controlling a video game with potentiometers, or a glove with accelerometers, or with ultrasound, etc.

Finally, another possible use is to automate tasks on devices. For example, if you have to run a process on many computers, or enter a password discreetly, you can configure one or several devices to execute the processes. You just plug them in, and that’s it.

As we said, a very easy-to-use and sometimes forgotten functionality, which can be useful in quite a few situations and is well worth a quick post.

Arduino as a Keyboard

To use Arduino as a keyboard we have the ‘Keyboard’ library included in the standard Arduino IDE. Its use is very simple, here is a summary of the library’s main functions.

// Start and end the virtual keyboard
Keyboard.begin()
Keyboard.end()

// Write text using the keyboard
Keyboard.print()
Keyboard.println()
Keyboard.write()

// Press and release a key
Keyboard.press()
Keyboard.release()
Keyboard.releaseAll()
Copied!

As we can see, its use is very similar to using the Serial library. But let’s see it with some examples.

Simple Keyboard Example

Let’s start with a simple example. The following code writes ‘Hello world!’ using keyboard emulation.

#include <Keyboard.h>

void setup() {
  Keyboard.begin();
  delay(5000);
}

void loop() {
  Keyboard.println("Hello world!");
  delay(1000);
}
Copied!

We have left a 5-second pause in the ‘Setup’, to make it easier to reprogram. If we don’t leave any pause, the program will start writing immediately, which would write in the IDE itself, and it’s annoying for reprogramming.

If you don’t put a pause and have problems reprogramming, you can do it by connecting the Arduino to the USB just when the IDE has finished compiling.

Keyboard Example with Key Combinations

If we want to use key combinations (for example, Control+…) we can use the ‘press’ and ‘release’ functions. In the following code we see two different ways to, for example, press Control+n.

#include <Keyboard.h>

void setup() {
  Keyboard.begin();
}

void loop() {
  Keyboard.press(KEY_LEFT_CTRL);
  Keyboard.press('n');
  delay(100);
  Keyboard.releaseAll();
  
  // another way to release, key by key
  //Keyboard.release(KEY_LEFT_CTRL);
  //Keyboard.release('n');
  
  delay(1000);
}
Copied!

Here is a list of the available keys and modifiers, and their correspondence in hexadecimal and decimal code.

KEYHEXDEC
KEY_LEFT_CTRL0x80128
KEY_LEFT_SHIFT0x81129
KEY_LEFT_ALT0x82130
KEY_LEFT_GUI0x83131
KEY_RIGHT_CTRL0x84132
KEY_RIGHT_SHIFT0x85133
KEY_RIGHT_ALT0x86134
KEY_RIGHT_GUI0x87135
KEY_UP_ARROW0xDA218
KEY_DOWN_ARROW0xD9217
KEY_LEFT_ARROW0xD8216
KEY_RIGHT_ARROW0xD7215
KEY_BACKSPACE0xB2178
KEY_TAB0xB3179
KEY_RETURN0xB0176
KEY_ESC0xB1177
KEY_INSERT0xD1209
KEY_DELETE0xD4212
KEY_PAGE_UP0xD3211
KEY_PAGE_DOWN0xD6214
KEY_HOME0xD2210
KEY_END0xD5213
KEY_CAPS_LOCK0xC1193
KEY_F10xC2194
KEY_F20xC3195
KEY_F30xC4196
KEY_F40xC5197
KEY_F50xC6198
KEY_F60xC7199
KEY_F70xC8200
KEY_F80xC9201
KEY_F90xCA202
KEY_F100xCB203
KEY_F110xCC204
KEY_F120xCD205

Keyboard Example Opening Notepad

In this last example, we see a somewhat more functional case, launching Notepad in Windows. To do this, we press the Windows+R key, type “notepad” + ENTER in the window that appears. Finally, we write “Hello world!” in the Notepad window.

#include <Keyboard.h>

void setup() {
  Keyboard.begin();
}

void loop() {
  Keyboard.press(KEY_RIGHT_GUI);
  Keyboard.press('r');
  delay(100);
  Keyboard.releaseAll();  
  delay(1000);
  
  Keyboard.println("notepad");
  
  Keyboard.press(KEY_RETURN);
  delay(100);
  Keyboard.releaseAll();
  
  Keyboard.print("Hello world!");
}
Copied!

Emulate Mouse with Arduino

It is also possible to emulate a mouse with the ‘Mouse’ library included in the standard IDE. The functions of this library are as follows.

// Start and stop the virtual mouse
Mouse.begin()
Mouse.end()

// Relative mouse movement
Mouse.move()

// Click with the mouse
Mouse.click()
Mouse.press()
Mouse.release()
Mouse.isPressed()
Copied!

Simple Mouse Example

Let’s see a simple example of how to emulate a mouse with Arduino. The following example moves the mouse 10px left and up and clicks.

#include "Mouse.h"

void setup() {
  Mouse.begin();
}

void loop() {
  Mouse.move(10, 10, 0);
  Mouse.click();
  delay(100);
}
Copied!

Enhanced Mouse Library

The ‘Mouse’ library has the disadvantage that it only allows relative mouse movements. This may be sufficient for interacting with programs (e.g., a video game) from a device moved by a user (e.g., a potentiometer or a gyroscope).

But in many cases, it is convenient to be able to move the mouse to specific screen coordinates, in absolute value. For example, to click a button in a program.

For this, we have the library https://github.com/per1234/MouseTo which incorporates many features compared to the ‘Mouse’ library. The library has several examples that you can review to see its use.