# Neo Trinkey CircuitPython Rubber Ducky

## Overview

![](https://cdn-learn.adafruit.com/assets/assets/000/103/754/medium800/circuitpython_4870-00.jpg?1628270243)

In this project, you'll learn how to use your Neo Trinkey and the new [Adafruit CircuitPython Ducky](https://github.com/adafruit/Adafruit_CircuitPython_Ducky) library to run a DuckyScript file. [DuckyScript](https://archive.ph/YwFhy) is a language that can be used on USB rubber duckies.

Rubber duckies are devices that you can plug into a computer to send a predefined set of keystrokes. They are usually considered hacking tools, and there's no question that if you have physical access to a computer, rubber duckies are an extremely fast and efficient way to do something, but they can also be very useful for running macros to save you time.

This project will turn your Trinkey into one of those rubber duckies. This project can be done with any of the Trinkeys, but I'd personally recommend the [Neo Trinkey](https://www.adafruit.com/product/4870) or the [NeoKey Trinkey](https://www.adafruit.com/product/5020). This guide will use the Neo Trinkey, but assuming you already have CircuitPython installed, the instructions and code should be exactly the same.

Disclaimer: This project is for fun. Don't hack people unless they say you can.

![](https://cdn-learn.adafruit.com/assets/assets/000/103/753/medium800thumb/circuitpython_hackerman.jpg?1628265905)

## Parts
Featured
### Adafruit Neo Trinkey - SAMD21 USB Key with 4 NeoPixels

[Adafruit Neo Trinkey - SAMD21 USB Key with 4 NeoPixels](https://www.adafruit.com/product/4870)
It's half USB Key, half Adafruit Trinket_..._it's **Neo Trinkey** , the circuit board with a Trinket M0 heart and&nbsp; four RGB NeoPixels for customizable glow. We were inspired by some USB key flashlight boards that would turn any battery pack into an LED torch. So...

In Stock
[Buy Now](https://www.adafruit.com/product/4870)
[Related Guides to the Product](https://learn.adafruit.com/products/4870/guides)
![Close-up of NeoTrinkey PCB inserted into a laptop. The LEDs emit rainbow colors.](https://cdn-shop.adafruit.com/640x480/4870-03.jpg)

Featured
### Adafruit NeoKey Trinkey - USB NeoPixel Mechanical Key Switch

[Adafruit NeoKey Trinkey - USB NeoPixel Mechanical Key Switch](https://www.adafruit.com/product/5020)
It's half USB Key, half Adafruit Trinket, half mechanical keeb_..._it's **NeoKey Trinkey** , the circuit board with a Trinket M0 heart, a NeoPixel glow, and a Cherry MX-compatible. We were inspired by single-key macro pads we've seen. So we thought, hey what if...

In Stock
[Buy Now](https://www.adafruit.com/product/5020)
[Related Guides to the Product](https://learn.adafruit.com/products/5020/guides)
![Video of a person pressing the NeoKey Trinkey which emits rainbow colors.](https://cdn-shop.adafruit.com/product-videos/640x480/5020-06.jpg)

# Neo Trinkey CircuitPython Rubber Ducky

## Project Code

This guide assumes you already have CircuitPython installed on your Trinkey. The Trinkey ships with CircuitPython pre-installed, so if you have put Arduino on it or need to re-install CircuitPython, [check out this guide](https://learn.adafruit.com/adafruit-neo-trinkey/circuitpython).

This project only uses built-in libraries, so all you have to do is plug your Trinkey into your computer, click **Download Project Bundle** below and unzip it.

Warning: This code will not work on Gemma M0, Trinket M0 or Feather M0 non-Express as the function time.monotonic_ns() is not available for those boards.
If you get that or a message on No long integer support, please post to https://forums.adafruit.com/

Now, copy&nbsp; **code.py** and **duckyscript.txt** over to your Trinkey and you should be all set.

You can use the script included in the project bundle which can be used to enroll a Chromebook, or you can make your own. You can find the [syntax and available commands](https://github.com/hak5darren/USB-Rubber-Ducky/wiki/Duckyscript) and [a bunch of example scripts](https://github.com/hak5darren/USB-Rubber-Ducky/wiki) in the wiki.

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

## Usage

To use this device, simply plug it into the target computer and press the key switch. You can find a number of other scripts and attacks here:

[Rubber Ducky Payloads](https://github.com/hak5/usbrubberducky-payloads)
### Non-US Keyboard Layouts

A library of non-US keyboard layouts [is available](https://github.com/Neradoc/Circuitpython_Keyboard_Layouts) if you want to adapt this for other keyboards.

## Code Run-Through
First, the code imports the required libraries.

```python
import time

import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
import adafruit_ducky

import touchio # pylint: disable=unused-import
import board
import neopixel
from digitalio import DigitalInOut, Pull # pylint: disable=unused-import
```

The code next sets up the buttons used to control the script and the NeoPixels used to indicate the status of the script.

```python
# Uncomment for Neo Trinkey
touch1 = touchio.TouchIn(board.TOUCH1)
touch2 = touchio.TouchIn(board.TOUCH2)

# Uncomment for NeoKey Trinkey
#button = DigitalInOut(board.SWITCH)
#button.switch_to_input(pull=Pull.DOWN)
#button_state = False

pixels = neopixel.NeoPixel(board.NEOPIXEL, 4)

pixels.fill((0xFFFFFF))
```

Then, the code sets up the objects required to emulate a keyboard.

```python
time.sleep(1)  # Sleep for a bit to avoid a race condition on some systems
keyboard = Keyboard(usb_hid.devices)
keyboard_layout = KeyboardLayoutUS(keyboard)  # We're in the US :)
```

Before the ducky script is run, the code needs to initialize the object it will use to send the keystrokes in the script.

```python
duck = adafruit_ducky.Ducky("duckyscript.txt", keyboard, keyboard_layout)
```

Finally, the code runs the script. It checks if the control button has been pressed and if it has, it runs the loop. If the button is pressed again, the script is paused.

```python
while result is not False:
    #if button.value: # Uncomment for NeoKey Trinkey
    if any([touch1.value, touch2.value]): # Uncomment for Neo Trinkey
        running = not running
        if running:
            pixels.fill((0x00FF00))
        else:
            pixels.fill((0xFF0000))
        time.sleep(0.2)
    if running:
        result = duck.loop()
```

## Using the Rubber Ducky

To use it, simply plug it into a computer and press the control button to run it. You'll know it's running when the NeoPixels turn green. When the script is done running, the NeoPixels will return to being white.


## Guide Products

### Adafruit Neo Trinkey - SAMD21 USB Key with 4 NeoPixels

[Adafruit Neo Trinkey - SAMD21 USB Key with 4 NeoPixels](https://www.adafruit.com/product/4870)
It's half USB Key, half Adafruit Trinket_..._it's **Neo Trinkey** , the circuit board with a Trinket M0 heart and&nbsp; four RGB NeoPixels for customizable glow. We were inspired by some USB key flashlight boards that would turn any battery pack into an LED torch. So...

In Stock
[Buy Now](https://www.adafruit.com/product/4870)
[Related Guides to the Product](https://learn.adafruit.com/products/4870/guides)
### Adafruit NeoKey Trinkey - USB NeoPixel Mechanical Key Switch

[Adafruit NeoKey Trinkey - USB NeoPixel Mechanical Key Switch](https://www.adafruit.com/product/5020)
It's half USB Key, half Adafruit Trinket, half mechanical keeb_..._it's **NeoKey Trinkey** , the circuit board with a Trinket M0 heart, a NeoPixel glow, and a Cherry MX-compatible. We were inspired by single-key macro pads we've seen. So we thought, hey what if...

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

## Related Guides

- [Adafruit Neo Trinkey](https://learn.adafruit.com/adafruit-neo-trinkey.md)
- [Adafruit NeoKey Trinkey](https://learn.adafruit.com/adafruit-neokey-trinkey.md)
- [Neo Trinkey Case](https://learn.adafruit.com/neo-trinkey-case.md)
- [Neo Trinkey Zoom Shortcuts](https://learn.adafruit.com/neo-trinkey-zoom-shortcuts.md)
- [Neo Trinkey Auto Screen Locker](https://learn.adafruit.com/neo-trinkey-auto-screen-locker.md)
- [Adafruit MCP9601 I2C Thermocouple Amplifier](https://learn.adafruit.com/adafruit-mcp9601.md)
- [Sound Reactive Rapunzel Hair](https://learn.adafruit.com/sound-reactive-rapunzel-hair.md)
- [MIDI Laser Harp with Time of Flight Distance Sensors](https://learn.adafruit.com/midi-laser-harp-time-of-flight-sensors.md)
- [Adafruit CCS811 Air Quality Sensor](https://learn.adafruit.com/adafruit-ccs811-air-quality-sensor.md)
- [Adafruit QT Py RP2040](https://learn.adafruit.com/adafruit-qt-py-2040.md)
- [CircuitPython BLE Advertising Beacons](https://learn.adafruit.com/circuitpython-ble-advertising-beacons.md)
- [PyPortal IoT Data Logger with Analog Devices ADT7410, Adafruit IO and CircuitPython](https://learn.adafruit.com/iot-pyportal-data-logger-adafruitio-circuitpython.md)
- [Adafruit PiCowbell DVI Output](https://learn.adafruit.com/adafruit-picowbell-dvi-output.md)
- [CircuitPython Libraries on any Computer with Raspberry Pi Pico](https://learn.adafruit.com/circuitpython-libraries-on-any-computer-with-raspberry-pi-pico.md)
- [EZ Make Oven](https://learn.adafruit.com/ez-make-oven.md)
