Language: EN

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

How to emulate a keyboard or mouse with Arduino

A little-known functionality of certain microprocessors is the ability to emulate a keyboard or mouse when connected to a computer and other compatible devices via USB.

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

Other processors outside of the Arduino “ecosystem” may also incorporate the HID emulation function. There are even certain ‘workarounds’ for processors like the Atmega 328 to act as a HID, but in general, they do not work very well.

What can this be useful for? For example, to interact with machines or programs to which we do not have access to install a program. For example, we can control an Android video player, or even industrial machines that cannot have software installed but are controlled by keyboard or mouse.

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

Finally, another possible use is to automate tasks in devices. For example, if you have to run a process on many computers, or enter a password discreetly, you can set up one or more devices to run the processes. They plug it in, and it’s ready.

As we say, a very simple functionality to use and sometimes forgotten, which can be useful in quite a few situations and deserves a quick entry.

Arduino as a keyboard

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

// 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()

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. With the following code we write ‘Hello world!’ using keyboard emulation.

#include <Keyboard.h>

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

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

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 is annoying to reprogram.

If you don’t put a pause and have trouble reprogramming, you can do it by connecting the Arduino to 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);
}

Here is a list of the available keys and modifiers, and their hexadecimal and decimal codes.

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 open 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!");
}

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()

Simple mouse example

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

#include "Mouse.h"

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

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

Enhanced mouse library

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

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

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