# Make It a Mouse

## Overview

![](https://cdn-learn.adafruit.com/assets/assets/000/058/391/medium800thumb/makecode_mouse.jpg?1533045742)

There are a lot of things one can do if they are able to interface their electronics to their desktop / laptop computer. It's easier than ever to make these connections, especially with Circuit Playground Express.

The USB specification has several ways to characterize devices. This includes telling the computer that the device you are plugging into the USB port is a [keyboard](https://learn.adafruit.com/make-it-a-keyboard "Make It a Keyboard") or mouse. These peripherals are categorized as human interface devices (HID).&nbsp;

Many Adafruit products can communicate with other computers using HID and emulating a mouse.

What types of projects work well emulating a mouse? Some examples:

- Assistive Technology - the ability to trigger keyboard input using a multitude of interface devices connected to the microcontroller
- Simulating mouse clicks with another device.
- Making a mouse jiggler to keep a computer from falling asleep

This guide will show you how to quickly and easily emulate a mouse on an Adafruit Circuit Playground Express board in both Microsoft MakeCode and CircuitPython.

## Parts List
Featured
### Circuit Playground Express

[Circuit Playground Express](https://www.adafruit.com/product/3333)
 **Circuit Playground Express** is the next step towards a perfect introduction to electronics and programming. We've taken the original Circuit Playground Classic and made it even better! Not only did we pack even more sensors in, we also made it even easier to...

In Stock
[Buy Now](https://www.adafruit.com/product/3333)
[Related Guides to the Product](https://learn.adafruit.com/products/3333/guides)
![A Black woman's manicured hand holds a round microcontroller with lit up LEDs.](https://cdn-shop.adafruit.com/640x480/3333-05.jpg)

Featured
### USB cable - USB A to Micro-B

[USB cable - USB A to Micro-B](https://www.adafruit.com/product/592)
This here is your standard A to micro-B USB cable, for USB 1.1 or 2.0. Perfect for connecting a PC to your Metro, Feather, Raspberry Pi or other dev-board or microcontroller

Approximately 3 feet / 1 meter long

In Stock
[Buy Now](https://www.adafruit.com/product/592)
[Related Guides to the Product](https://learn.adafruit.com/products/592/guides)
![USB cable - USB A to Micro-B - 3 foot long](https://cdn-shop.adafruit.com/640x480/592-01.jpg)

# Make It a Mouse

## Microsoft MakeCode

If you are new to Microsoft MakeCode, you can&nbsp;[learn the basics of MakeCode here](https://learn.adafruit.com/makecode "MakeCode Basics").

To add keyboard support, MakeCode needs to load an extension - to bring in some blocks that are not usually loaded in the main program. To do this, click on the **ADVANCED** blog group:

![](https://cdn-learn.adafruit.com/assets/assets/000/058/108/medium800/makecode_advanced.png?1532623834)

Then click on the EXTENSION block:

![](https://cdn-learn.adafruit.com/assets/assets/000/058/110/medium800/makecode_Extensions.png?1532623852)

And select the **mouse** - Mouse emulation over HID extension:

![](https://cdn-learn.adafruit.com/assets/assets/000/058/112/medium800/makecode_mouse-extension.png?1532623887)

This adds a new block group on the main MakeCode page called **MOUSE** with several new functional blocks when you click the group button:

![](https://cdn-learn.adafruit.com/assets/assets/000/058/115/medium800/makecode_mouse-blocks.png?1532624064)

At this point you will want to define some action that lets your program know you wish to use one of the mouse functions. This can be anything but for example you may want to trigger mouse input on:

- Press of the A or B buttons or the slide switch
- Press of a capacitive touch pad (marked A1 to A7)
- On a loud sound, shake of the board, or bright light
- Periodically move the mouse after several seconds, minutes, hours, etc.

For a simple demonstration, here are four button event blocks from the **INPUT** block group `on button click`&nbsp;- the A button on Circuit Playground Express clicks the left mouse button (down when A is down, up when A is up). If Button B is pressed, it simulates the right mouse button clicked down, up when B is released.&nbsp;

Warning: 

![](https://cdn-learn.adafruit.com/assets/assets/000/058/389/medium800/makecode_mouse-buttons.png?1533045669)

[Load this Program in MakeCode](https://makecode.com/_iY942F5ceARR)
## Moving the Cursor

You can also define moving the cursor. Ideas include:

- Using the Circuit Playground Express accelerometer tilt
- Using buttons or capacitive touch on the outside pads

The following example uses the capacitive touch pads to move in the x and y direction.

Pads **A3** and **A4** move the mouse horizontally, pads **A7** and **A1** move the cursor vertically.

![](https://cdn-learn.adafruit.com/assets/assets/000/058/390/medium800/makecode_Capture.png?1533045678)

[Load this Program in MakeCode](https://makecode.com/_L5g7U17cdCpk)
Here is the code in action:

![](https://cdn-learn.adafruit.com/assets/assets/000/058/487/medium800thumb/makecode_mouse.jpg?1533141437)

# Make It a Mouse

## CircuitPython

![](https://cdn-learn.adafruit.com/assets/assets/000/058/485/medium800/makecode_circuitpython_circuit_playground_adafruit_blinka_computer.png?1533139701)

If you are new to CircuitPython, we recommend you start with the [Welcome to CircuitPython Guide](https://learn.adafruit.com/welcome-to-circuitpython?view=all) then come back here.

Adafruit recommends installing and using the Mu editor, available for Windows, macOS, and Linux. You can [learn about Mu in this guide](https://learn.adafruit.com/welcome-to-circuitpython/installing-mu-editor "Mu Editor").

## The adafruit\_hid Library

Adafruit has developed libraries to easily use CircuitPython devices which have HID capabilities as mouse devices. There are several functions you may wish to use in your programs, this guide will show them with examples.

Warning: 

## The adafruit\_circuitplayground Library

Adafruit has developed a library to access much of the functionality on the Circuit Playground Express called `adafruit_circuitplayground`. You can use the library by defining&nbsp;  
`from adafruit_circuitplayground.express import cpx` at the start of your code. The library has functions for all the sensors, buttons, etc. The examples will use `cpx.button_a` and `cpx.button_b` to read the pushbuttons.

## Performing Mouse Clicks

The following performs a mouse left click when Circuit Playground Express Button A is pressed, a right click when Button B is pressed:

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Make_It_A-Mouse/cpx-mouse-buttons/code.py

When a button is pressed, the code sends the mouse button press notification. It then waits, doing nothing while the button is still pressed. When the button is not pressed anymore, the code sends a mouse release button call.

## Moving the Mouse Cursor

The following uses 4 of the capacitive touch pads on the Circuit Playground Express to signal having the mouse move in the +x, +y, -x, and -y axis. This can be changed to switch activations or other methods such as using the accelerometer chip to move based on tilt of the Circuit Playground circuit board.

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Make_It_A-Mouse/mouse-cursor/code.py

## Documentation

The online documentation for the Adafruit HID library is[available at on readthedocs](https://circuitpython.readthedocs.io/projects/hid/en/latest/).

Adafruit has [another guide](https://learn.adafruit.com/circuitpython-made-easy-on-circuit-playground-express "CPX Library") that goes over the functionality of the adafruit\_circuitplayground (CPX) library functions.

## Going Further

The mouse keys and cursor code can be merged to one overall mouse program with buttons and cursor control.&nbsp;

This type of work is terrific for assistive technologies projects.

You can also make a mouse jiggler program which uses the `time` library along with the `mouse` library to move the mouse a little, sleep, then do it again. This may keep a computer awake if it has auto shutdown which you cannot change.


## Guide Products

### Circuit Playground Express

[Circuit Playground Express](https://www.adafruit.com/product/3333)
 **Circuit Playground Express** is the next step towards a perfect introduction to electronics and programming. We've taken the original Circuit Playground Classic and made it even better! Not only did we pack even more sensors in, we also made it even easier to...

In Stock
[Buy Now](https://www.adafruit.com/product/3333)
[Related Guides to the Product](https://learn.adafruit.com/products/3333/guides)
### USB cable - USB A to Micro-B

[USB cable - USB A to Micro-B](https://www.adafruit.com/product/592)
This here is your standard A to micro-B USB cable, for USB 1.1 or 2.0. Perfect for connecting a PC to your Metro, Feather, Raspberry Pi or other dev-board or microcontroller

Approximately 3 feet / 1 meter long

In Stock
[Buy Now](https://www.adafruit.com/product/592)
[Related Guides to the Product](https://learn.adafruit.com/products/592/guides)

## Related Guides

- [Adafruit Circuit Playground Express](https://learn.adafruit.com/adafruit-circuit-playground-express.md)
- [SpaceX Helmet](https://learn.adafruit.com/spacex-helmet.md)
- [Capacitive Touch Pulsing Heart](https://learn.adafruit.com/3d-printed-heart-capacitive-touch.md)
- [Animatronic Hand](https://learn.adafruit.com/animatronic-hands.md)
- [Crickit Powered Maker Ice Cream Truck!](https://learn.adafruit.com/crickit-maker-ice-cream-truck.md)
- [PyPortal GitHub Stars Trophy](https://learn.adafruit.com/pyportal-github-stars-trophy.md)
- [Using Dashblock to Create Custom APIs for PyPortal](https://learn.adafruit.com/using-dashblock-to-create-custom-apis-for-pyportal.md)
- [Chatty Light-Up Circuit Playground Express Mask](https://learn.adafruit.com/chatty-light-up-cpx-mask.md)
- [Launch Deck Trellis M4](https://learn.adafruit.com/launch-deck-trellis-m4.md)
- [Halloween Sentry-Bot with CRICKIT for CPX](https://learn.adafruit.com/halloween-sentry-bot.md)
- [Sparkle Skirt Playground](https://learn.adafruit.com/sparkle-skirt-playground.md)
- [Where's My Friend? A Location-Aware Display with PyPortal and ItsASnap](https://learn.adafruit.com/where-s-my-friend-a-location-display-frame-with-pyportal.md)
- [Lucky Cat with Circuit Playground Express](https://learn.adafruit.com/lucky-cat-with-circuit-playground-express.md)
- [NeoTrellis M4 Animated Dice Roller](https://learn.adafruit.com/neotrellis-dice.md)
- [Circuit Playground Powerpuff Girls Valentine](https://learn.adafruit.com/circuit-playground-valentine.md)
- [Simon Game for PyRuler and CircuitPython](https://learn.adafruit.com/simon-game-with-pyruler-and-circuitpython.md)
