# Make It a Keyboard

## Overview

![](https://cdn-learn.adafruit.com/assets/assets/000/058/387/medium800thumb/makecode_keyboard.jpg?1533043264)

There are a great number of things one can do if they are able to interface their electronics to their desktop / laptop computer. This process used to be kinda hard but it isn't now, thanks to USB!

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 or [mouse](https://learn.adafruit.com/make-it-a-mouse "Make It a Mouse"). These peripherals are categorized as human interface devices (HID).&nbsp;

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

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

- **Macro keys** - activating to send a predefined sequence of keystrokes
- **Assistive Technology** - the ability to trigger keyboard input using a multitude of interface devices connected to the microcontroller
- **Device control** - the ability to send multimedia commands for volume, etc.
- **Sensor input** - Turn a banana or some slime into a keyboard with the use of capacitive touch
- **Fun** - a simple prank would send some characters to someone's screen at random times

This guide will show you how to quickly and easily emulate a keyboard 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...

Out of 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 Keyboard

## 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 - bring in some blocks not in the main program. Click on the **ADVANCED** blog group:

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

Then click on the **EXTENSION** block:

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

And select the **keyboard** - Keyboard emulation over HID extension:

![](https://cdn-learn.adafruit.com/assets/assets/000/058/102/medium800/makecode_keyboard-extension.png?1532608572)

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

![](https://cdn-learn.adafruit.com/assets/assets/000/058/103/medium800/makecode_keyboard-blocks.png?1532608663)

At this point you will want to define some action that lets your program know you wish to use one of the Keyboard functions. This can be anything but for example you may want to trigger keyboard 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 send a key after several seconds, minutes, hours, etc.

For a simple demonstration, here are two button event blocks from the **INPUT** block group `on button click`&nbsp;- the A button on Circuit Playground Express prints ABC as text, it can be any text you want to type with only a button press. If Button B is pressed, if you have a music app playing it will pause playing - it works for sure if you have iTunes or Spotify, but not all apps listen for the media key.

![](https://cdn-learn.adafruit.com/assets/assets/000/058/483/medium800/makecode_mc1.png?1533138470)

[Download this Example in MakeCode](https://makecode.com/_fm06wzJPehbq)
Feel free to define other actions and have fun with your new keyboard!

## Frequently Asked Questions

**Do I have to unplug my other keyboard?**

No, all modern operating systems allow more than one keyboard to be connected at a time.

**How can I see the Circuit Playground Express as a device?**

For Windows, open up the Control Panel application, go to Devices and Printers then look under the Devices group for a keyboard icon labeled "PXT Device":

![](https://cdn-learn.adafruit.com/assets/assets/000/058/105/medium800/makecode_windows.png?1532610164)

# Make It a Keyboard

## CircuitPython

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

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 keyboard devices. There are several functions you may wish to use in your programs, this guide will show them with examples.

On the Circuit Playground Express, this library is _built-in!_ On other CircuitPython boards [you may have to install the library by hand, see this guide on how to do that](https://learn.adafruit.com/welcome-to-circuitpython/circuitpython-libraries), it's easy and just requires dragging a file onto your board.

## 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.

# Sending Individual Keys

A keyboard typically has well over a hundred keys with various combinations of shift, ctrl, alt, and caps. CircuitPython can send all of these but you need to know what you want to send. The following sends a capital A on Button A and a ctrl X on Button B:

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Make_It_A_Keyboard/keycodes/code.py

If a button is pushed, `kbd.send` sends a defined key. Then the `while`&nbsp;loop waits while the button is still down before checking for a new key to prevent rollover, getting a lot of sent characters for one button press.

For a list of all the keys and codes you can send, see [this code](https://github.com/adafruit/Adafruit_CircuitPython_HID/blob/master/adafruit_hid/keycode.py).

# Send Multimedia Keys

You can also send multimedia keycodes. The `adafruit_hid.consumer_control` library provides the functionality and the `adafruit_hid.consumer_control_code`&nbsp;library provides predefined values for multimedia keys.

The keys you can select are defined in [this Read The Docs page](http://circuitpython.readthedocs.io/projects/hid/en/latest/api.html#adafruit-hid-consumer-control-code-consumercontrolcode).

The following program will increment and decrement the volume on a USB controllable multimedia device (such as my Windows 10 PC) using the Circuit Playground Express Buttons A and B. The program will only send one code per press, if the button is held down it only sends one code due to the `while` statement. The `while` statement blocks can be removed to simulate press and hold to change volume.

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Make_It_A_Keyboard/keyboard-multimedia/code.py

# Sending Multiple Characters at Once

Here is an example of sending out a string of characters at once:

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Make_It_A_Keyboard/strings/code.py

## Keyboard Layouts

The `adafruit_hid` library currently only supports the US keyboard layout with `layout.write()`. However, layouts for keyboards for other languages and regions are available at [https://github.com/Neradoc/Circuitpython\_Keyboard\_Layouts](https://github.com/Neradoc/Circuitpython_Keyboard_Layouts).

# 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

Besides defining your own keyboard, there are some other applications of this technology. Adafruit has another guide which demonstrates using Circuit Playground Express as a keyboard for Android devices. Then, using the Android Google keyboard GBoard, it can convert Morse Code to text. If this interests you, see the guide&nbsp;[Android GBoard Morse Code Control with Circuit Playground Express](https://learn.adafruit.com/android-gboard-morse-code-at-with-circuitplayground-express).


## 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...

Out of 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)
- [NeoTrellis M4 MIDI File Synthesizer](https://learn.adafruit.com/neotrellism4-midi-file-player-synthesizer.md)
- [HalloWing M4 Tombstone](https://learn.adafruit.com/hallowing-tombstone.md)
- [The MonkMakes Plant Monitor and CircuitPython](https://learn.adafruit.com/monkmakes-plant-monitor-and-circuitpython.md)
- [Adafruit Audio FX Sound Board](https://learn.adafruit.com/adafruit-audio-fx-sound-board.md)
- [Astrolite NeoPixel Upgrade](https://learn.adafruit.com/astrolite-neopixel-upgrade.md)
- [No-Solder LED Disco Tie with Bluetooth](https://learn.adafruit.com/no-solder-circuit-playground-bluetooth-disco-tie.md)
- [MakeCode for Circuit Playground Express](https://learn.adafruit.com/makecode.md)
- [Digi-Key IoT Studio Mini Smart Home](https://learn.adafruit.com/digikey-iot-studio-smart-home.md)
- [Sensors in MakeCode](https://learn.adafruit.com/sensors-in-makecode.md)
- [Chilled Drinkibot](https://learn.adafruit.com/chilled-drinkibot.md)
- [Adventure Time Coffee Cup Lamp with MakeCode](https://learn.adafruit.com/adventure-time-coffee-cup-lamp.md)
- [Easy Sparkle Pocket T-Shirt](https://learn.adafruit.com/easy-sparkle-pocket-t-shirt.md)
- [Introducing Adafruit Crickit #MakeRobotFriend](https://learn.adafruit.com/adafruit-crickit-creative-robotic-interactive-construction-kit.md)
- [Trellis M4 Beat Sequencers](https://learn.adafruit.com/trellis-m4-beat-sequencer.md)
- [NeoTrellis Tabletop RPG Soundboard](https://learn.adafruit.com/neotrellis-dungeon-crawl-soundboard.md)
