Keyboard/Mouse Control with Arduino Leonardo
If you need to send keyboard/mouse commands to a computer for your project, you will need to use one of my Arduino Leonardos instead of the Arduino Unos that came with your kits. The Arduino Leonardo has a special chip that is full USB HID-compliant that acts like a keyboard and/or mouse. Once you get the basics of your project working with your Arduino Uno, I will lend you an Arduino Leonardo for your project. The information below describes how to use the necessary libraries to send commands to a computer.
Arduino Leonardo
The Arduino Leonardo is identical in pinout to the Arduino Uno and fairly similar in functionality. The one major difference is that we can use the "HID-Project" library to send mouse and keyboard commands to a computer.
You will need to select the dropdown in Arduino Create to compile code for an Arduino Leonardo instead of an Arduino Uno. See the image below for more info:
HID Project Library
GitHub Repo: https://github.com/NicoHood/HID
Documentation: https://github.com/NicoHood/HID/wiki
Keyboard
Use Keyboard for sending keyboard commands for typical keys.
Keyboard Functions
All of the Keyboard functions are described here: https://www.arduino.cc/reference/en/language/functions/usb/keyboard/.
- Keyboard.begin() - call this function once in setup to begin emulating a keyboard.
- Keyboard.end() - call this function to stop emulating a keyboard.
- Keyboard.press(key) - call this function to press and hold a single key until told to release.
key
: the key to press.
- Type as a single letter in single quotation marks. Examples: 'C' or 'c' or ' ' or '!' or '1' or 'x'.
- Or type as special values shown below (no single quotes). KEY_LEFT_CTRL or KEY_RIGHT_ALT or KEY_TAB or KEY_ARROW_UP.
- Keyboard.print(characters) - call this function to write a bunch of text to the screen at once.
characters
: a string to be sent to the computer as a keystroke. Must be written between double quotes. Examples: "hello!" or "username" or "password1234".
- Keyboard.release(key) - call this function to release a single key.
key
: the key to release.
- Type as a single letter in single quotation marks. Examples: 'C' or 'c' or ' ' or '!' or '1' or 'x'.
- Or type as special values shown below (no single quotes). KEY_LEFT_CTRL or KEY_RIGHT_ALT or KEY_TAB or KEY_ARROW_UP
- Keyboard.releaseAll() - call this function to release all keys that have been pressed.
- Keyboard.write(key) - call this function to press and release a single key.
key
: the key to press and release.
- Type as a single letter in single quotation marks. Examples: 'C' or 'c' or ' ' or '!' or '1' or 'x'.
- Or type as special values shown below (no single quotes). KEY_LEFT_CTRL or KEY_RIGHT_ALT or KEY_TAB or KEY_ARROW_UP
Special Keys for "Keyboard" Object.
To use these special keys on a keyboard, you must write the values as shown below.
- KEY_LEFT_CTRL
- KEY_LEFT_SHIFT
- KEY_LEFT_ALT
- KEY_LEFT_GUI
- KEY_RIGHT_CTRL
- KEY_RIGHT_SHIFT
- KEY_RIGHT_ALT
- KEY_RIGHT_GUI
- KEY_UP_ARROW
- KEY_DOWN_ARROW
- KEY_LEFT_ARROW
- KEY_RIGHT_ARROW
- KEY_BACKSPACE
- KEY_TAB
- KEY_RETURN
- KEY_ESC
- KEY_INSERT
- KEY_DELETE
- KEY_PAGE_UP
- KEY_PAGE_DOWN
- KEY_HOME
- KEY_END
- KEY_CAPS_LOCK
- KEY_F1
- KEY_F2
- KEY_F3
- KEY_F4
- KEY_F5
- KEY_F6
- KEY_F7
- KEY_F8
- KEY_F9
- KEY_F10
- KEY_F11
- KEY_F12
- KEY_F13
- KEY_F14
- KEY_F15
- KEY_F16
- KEY_F17
- KEY_F18
- KEY_F19
- KEY_F20
- KEY_F21
- KEY_F22
- KEY_F23
- KEY_F24
Consumer
Use Consumer for sending keyboard commands for special keys like media keys (volume control, etc.). Almost identical in functionality to Keyboard, but can be used with special keys.
Consumer Functions
- Consumer.begin() - call this function once in setup to begin emulating a keyboard.
- Consumer.end() - call this function to stop emulating a keyboard.
- Consumer.press(key) - call this function to press and hold a single key until told to release.
key
: the key to press. Use special values shown below. Examples: MEDIA_VOLUME_UP or MEDIA_VOLUME_MUTE or CONSUMER_BROWSER_REFRESH.
- Consumer.release(key) - call this function to release a single key.
key
: the key to release. Use special values shown below. Examples: MEDIA_VOLUME_UP or MEDIA_VOLUME_MUTE or CONSUMER_BROWSER_REFRESH.
- Consumer.releaseAll() - call this function to release all keys that have been pressed.
- Consumer.write(key) - call this function to press and release a single key.
key
: the key to press and release. Use special values shown below. Examples: MEDIA_VOLUME_UP or MEDIA_VOLUME_MUTE or CONSUMER_BROWSER_REFRESH.
Special Keys for "Consumer" Object
To use these special keys on a keyboard, you must write the values as shown below.
- MEDIA_FAST_FORWARD
- MEDIA_REWIND
- MEDIA_NEXT
- MEDIA_PREVIOUS
- MEDIA_STOP
- MEDIA_PLAY_PAUSE
- MEDIA_VOLUME_MUTE
- MEDIA_VOLUME_UP
- MEDIA_VOLUME_DOWN
- CONSUMER_EMAIL_READER
- CONSUMER_CALCULATOR
- CONSUMER_EXPLORER
- CONSUMER_BROWSER_HOME
- CONSUMER_BROWSER_BACK
- CONSUMER_BROWSER_FORWARD
- CONSUMER_BROWSER_REFRESH
- CONSUMER_BROWSER_BOOKMARKS
Mouse
Use Mouse to send mouse commands.
Mouse Functions
All of the Mouse functions are described here: https://www.arduino.cc/reference/en/language/functions/usb/mouse/.
- Mouse.begin() - call this function once in setup to begin emulating a mouse.
- Mouse.click(button) - call this function to send a mouse click (press and release).
button
: which mouse button to press. Options are: MOUSE_LEFT
or MOUSE_RIGHT
or MOUSE_MIDDLE
- Mouse.end() - call this function to stop emulating a mouse.
- Mouse.move(xVal, yVal, wheel) - call this function to move the mouse cursor. The movement is always relative to the cursor's current location. If I say
- xVal: amount to move along the x-axis. Positive numbers move right. Negative numbers move left.
- yVal: amount to move along the y-axis. Positive numbers shift down. Negative numbers move up.
- wheel: amount to move scroll wheel.
- Mouse.press(button) - call this function to press and hold a mouse button.
button
: which mouse button to press. Options are: MOUSE_LEFT
or MOUSE_RIGHT
or MOUSE_MIDDLE
- Mouse.release() - call this function to release a mouse button that was held
button
: which mouse button to press. Options are: MOUSE_LEFT
or MOUSE_RIGHT
or MOUSE_MIDDLE
- Mouse.isPressed() - call this function to determine whether a button is pressed or not.
button
: which mouse button to press. Options are: MOUSE_LEFT
or MOUSE_RIGHT
or MOUSE_MIDDLE
- return value -
true
if pressed, false
if not
Example Code