Understanding Arduino Mouse and Keyboard Classes

To use the original Arduino system to emulate a mouse or keyboard you would simply include the HID library followed by either the mouse library or keyboard library or both like this.

Download: file
#include <HID.h>         //include the basic HID infrastructure first
#include <Mouse.h>
#include <Keyboard.h>

This would automatically create 2 global objects: one called Mouse and the other Keyboard. Here are the details of the public methods defined in Mouse.h.

Download: file
#define MOUSE_LEFT 1
#define MOUSE_RIGHT 2
#define MOUSE_MIDDLE 4
#define MOUSE_ALL (MOUSE_LEFT | MOUSE_RIGHT | MOUSE_MIDDLE)

class Mouse_
{
public:
  Mouse_(void);
  void begin(void);
  void end(void);
  void click(uint8_t b = MOUSE_LEFT);
  void move(signed char x, signed char y, signed char wheel = 0); 
  void press(uint8_t b = MOUSE_LEFT);   // press LEFT by default
  void release(uint8_t b = MOUSE_LEFT); // release LEFT by default
  bool isPressed(uint8_t b = MOUSE_LEFT); // check LEFT by default
};
extern Mouse_ Mouse;

The public methods are pretty much self-explanatory. The click method briefly presents and releases a mouse button. The default button if none is specified is the left button. The move method moves the mouse in the x (horizontal) and y (vertical) direction and an optional third parameter allows you to move the mouse wheel up or down. If you only want to move in one direction then the other parameters can be zero. The press and release methods allow you to press and hold a particular mouse button and only release it later. There is also a boolean method that allows you to test whether or not a particular button is currently pressed.

There is a begin and end method however close inspection of the actual code shows that they don't really do anything. But it's still a good idea to put a Mouse.begin(); in your setup section of your sketch.

Although "Mouse.begin();" is necessary with the original Arduino libraries, you MUST do a call to the begin method when using our conversion libraries.

Here are the details of the public methods of the Keyboard class.

Download: file
class Keyboard_ : public Print
{
public:
  Keyboard_(void);
  void begin(void);
  void end(void);
  size_t write(uint8_t k);
  size_t write(const uint8_t *buffer, size_t size);
  size_t press(uint8_t k);
  size_t release(uint8_t k);
  void releaseAll(void);
};
extern Keyboard_ Keyboard;

Again the methods are pretty much self-explanatory. There are 2 versions of the write method. One writes a single character while the other allows you to pass a pointer to a buffer full of characters with a length value. The press and release methods allow you to press and hold a particular key and then release it later. There is also a releaseAll method to release all pressed keys.

If the character you want to print is a standard printable ASCII character, you can simply send it using a character literal for example

Keyboard.write('A');

will write a capital letter "A" as if that key had been pressed. It takes care of upper and lowercase for you. However for non-printable characters you need to use special values for example

Keyboard.write(KEY_UP_ARROW);

will emulate the pressing of the up arrow key on your keyboard. A complete list of these values can be found on the Arduino.cc website at the link below and they are also in our libraries that we will be introducing later in the tutorial.

Arduino Keyboard Modifiers

For example if you want to do do a control-c you would do

Keyboard.press(KEY_LEFT_CTRL);
Keyboard.write('c');
Keyboard.release(KEY_LEFT_CTRL);

In addition to the methods listed above, note that this class is also connected to the Print class. That means you can also use statements like

Keyboard.print("This is a bunch of text");
Keyboard.println("This text has a new line at the end");
Keyboard.print("Here's a number:");
Keyboard.println(My_Value,DEC);

There is a "begin" and "end" method however close inspection of the actual code shows that they don't really do anything. But it's still a good idea to put a Keyboard.begin(); in your setup section of your sketch.

Although "Keyboard.begin();" is necessary with the original Arduino libraries, you MUST do a call to the begin method when using our conversion libraries.

More information about the Arduino libraries can be found in these links.

NOTE: The Mouse.h and Keyboard.h source code which we have quoted above contains the following copyright notice.

Download: file
/*
  Copyright (c) 2015, Arduino LLC
  Original code (pre-library): Copyright (c) 2011, Peter Barrett

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/
This guide was first published on May 13, 2020. It was last updated on May 13, 2020.
This page (Understanding Arduino Mouse and Keyboard Classes) was last updated on Jul 14, 2020.