Circuit Playground Express has the ability to emulate a keyboard and mouse, thus creating your own input devices.
While usually, that means laudable projects such as helping people who have physical challenges, or productivity automation, here I will use it for an infantile prank.
On a serious note, it will show you how to use the feature to make something useful too!
Not got a Circuit Playground Express?
Never fear!
Arduino developers can use the HID library, and also perform HID on 32u4 based boards, such as Due and Arduino Zero
Pi Zero owners can follow the Adafruit instructions
Circuit Python Code
First we must import the libraries that do all the heavy lifting. Adafruit have packaged up everything we need across the HID module, but for convenience, we break it all out.
As you can see, I am using US keyboard layout, but for this demo it doesn't really come into play.
import time
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.keycode import Keycode
from adafruit_hid.mouse import Mouse
Our loop will continue while the device is plugged in, which gets annoying very quickly!
We create mouse and keyboard objects, then send commands.
With the keyboard, we send a silly string, then release all keys.
WIth the mouse, we move down and right, and click both buttons.
while True:
time.sleep(1)
mouse = Mouse()
keyboard = Keyboard()
keyboard_layout = KeyboardLayoutUS(keyboard)
keyboard_layout.write("My Keyboard is haunted!")
keyboard.release_all()
time.sleep(1)
mouse.click(Mouse.LEFT_BUTTON)
mouse.click(Mouse.RIGHT_BUTTON)
mouse.move(x=1)
mouse.move(y=1)
I will warn you, even debugging this got me frustrated. If you didn't know where the mouse and keyboard control was coming from you would be driven insane.
AWESOME SKILL @makerhacks