树莓派pico入坑笔记,快捷键键盘制作

使用usb_hid功能制作快捷键小键盘,定义了6个键,分别是

ctrl+z ctrl+v ctrl+c

ctrl+a ctrl+w ctrl+n

对应引脚

board.GP4, board.GP8, board.GP13

board.GP28, board.GP20, board.GP17

需要用到的库,记得复制进单片机存储里面

然后是main主程序代码

python 复制代码
import board
from digitalio import DigitalInOut,Direction,Pull
import time
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.keycode import Keycode

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 :)

Led=DigitalInOut(board.LED)
Led.direction=Direction.OUTPUT
Led.value=False 
#引脚列表
kb=[board.GP4,board.GP8,board.GP13,board.GP28,board.GP20,board.GP17]
#存储配置后的引脚
KeyPin=[]
#功能键列表
key=[Keycode.Z,Keycode.V,Keycode.C,Keycode.A,Keycode.W,Keycode.N]
control_key = Keycode.CONTROL
#配置引脚
for i in kb:
    k=DigitalInOut(i)
    k.direction.INPUT
    k.pull=Pull.UP
    KeyPin.append(k)
#工作部分
while True :
    for i in KeyPin:
        if not i.value:
            Led.value=True
            while not i.value:
                pass
            keyboard.press(control_key, key[KeyPin.index(i)])
            keyboard.release_all()
            Led.value=False
            time.sleep(0.01)

        

boot启动程序代码,作用是防止被识别为usb存储设备

python 复制代码
# disabled usb diivide
import storage 
storage.disable_usb_drive()

更多hid设备参考以下教程

Adafruit HID 库

CircuitPython HID 键盘和鼠标

相关推荐
tingshuo291714 小时前
S001 【模板】从前缀函数到KMP应用 字符串匹配 字符串周期
笔记
西岸行者6 天前
学习笔记:SKILLS 能帮助更好的vibe coding
笔记·学习
starlaky6 天前
Django入门笔记
笔记·django
勇气要爆发6 天前
吴恩达《LangChain LLM 应用开发精读笔记》1-Introduction_介绍
笔记·langchain·吴恩达
悠哉悠哉愿意6 天前
【单片机学习笔记】串口、超声波、NE555的同时使用
笔记·单片机·学习
勇气要爆发6 天前
吴恩达《LangChain LLM 应用开发精读笔记》2-Models, Prompts and Parsers 模型、提示和解析器
android·笔记·langchain
qianshanxue116 天前
计算机操作的一些笔记标题
笔记
土拨鼠烧电路6 天前
笔记11:数据中台:不是数据仓库,是业务能力复用的引擎
数据仓库·笔记
土拨鼠烧电路6 天前
笔记14:集成与架构:连接孤岛,构建敏捷响应能力
笔记·架构
烟花落o6 天前
栈和队列的知识点及代码
开发语言·数据结构·笔记·栈和队列·编程学习