Last active
December 27, 2024 23:04
-
-
Save aleb/d73fb48f43392050e71f0418e4d50ba3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Emulate a USB keyboard with a MicroPython device such as Raspberry Pico. | |
import pyb | |
# Microsoft | |
VENDOR_ID = 0x045e | |
# Keyboard | |
PRODUCT_ID = 0xfff8 | |
# The default pins for I2C0 are SDA=GP4, SCL=GP5. | |
I2C_BUS = 0 | |
I2C_ADDR = 0xB2 | |
def press_key(key_code, delay_ms=100): | |
# Key down | |
keyboard.send(bytes([0x00, key_code, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])) | |
pyb.delay(delay_ms) | |
# Key up | |
keyboard.send(bytes([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])) | |
# Setup the USB of the board such that it can be used to emulate a USB HID | |
# representing a keyboard. | |
pyb.usb_mode('HID', vid=VENDOR_ID, pid=PRODUCT_ID, hid=pyb.hid_keyboard, high_speed=False) | |
keyboard = pyb.USB_HID() | |
i2c = pyb.I2C(I2C_BUS, pyb.I2C.PERIPHERAL, addr=I2C_ADDR) | |
# Get keycodes from the I2C controller and make them key presses. | |
while True: | |
try: | |
data = i2c.recv(1, timeout=100) | |
except: | |
continue | |
if data: | |
key_code = data[0] | |
press_key(key_code) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment