Using Raspberry Pi Pico as Hid Device to control Mouse and Keyboard

For this all one needs is a rpi pico and circuit python for rpi pico.

The Circuit Python for pico can be found over here: https://circuitpython.org/board/raspberry_pi_pico/

Download the uf2 file and drag it over to the pico.

Next: download this as zip from or “git clone” it : https://github.com/adafruit/Adafruit_CircuitPython_HID

Extract it. In there , there is a folder called adafruit_hid , copy that folder to the pico where circuit python is already installed.

remember to transfer the python file with name “code.py” to pico, to run on boot.

This is the example for Mouse:

import time
import board
from digitalio import DigitalInOut, Direction, Pull
import time
import usb_hid
from adafruit_hid.mouse import Mouse
 
mouse = Mouse(usb_hid.devices)


x=0
y=0
mouse.move(x,y) 
# This will move the mouse from it's current position to x pixels 
# and y pixels. 
# So if the current position is (100,200) it'll move to
# (100+x,100+y)


This can also be used with a button so that it keeps the screen of pc awake ,like this:

import time
import board
from digitalio import DigitalInOut, Direction, Pull
import time
import usb_hid
from adafruit_hid.mouse import Mouse
 
mouse = Mouse(usb_hid.devices)
btn = DigitalInOut(board.GP14)
btn.direction = Direction.INPUT
btn.pull = Pull.DOWN
i=0
on=False
def onRelease():
    
    bState=btn.value
    released=False
    if (bState): 
        bState = btn.value
        while bState:
            bState = btn.value
    
        return True;


while True:
    
    if onRelease():
        on=not on
    if on:
        mouse.move(5,5)
        time.sleep(0.1)
        mouse.move(-5,-5)
        time.sleep(0.1)
    time.sleep(0.1)
    
    

This is the example for Keyboard.

import time
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode

key_A=Keycode.A
key_Shift = Keycode.SHIFT
keyboard=Keyboard(usb_hid.devices)

time.sleep(2)
keyboard.press(key_Shift, key_A)
keyboard.release(key_Shift, key_A)

if you have any questions, email me at:

Published by Hriday Barot's Blog

I am Hriday Barot

6 thoughts on “Using Raspberry Pi Pico as Hid Device to control Mouse and Keyboard

  1. Hi Hriday,

    I read this blog with interest. Does solution act as an HID Proxy so I can use the Pico to wake up a sleeping PC?

    Best regards.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create your website with WordPress.com
Get started
%d bloggers like this: