Using rp2040 as a keyboard AND a mouse through micropython #17475
Replies: 2 comments
-
|
See also: micropython/micropython-lib#921 I also copied in this question into github copilot, and the response that I got was more or less that it is currently not supported in micropython, and it offered to help me write a combined keyboard and mouse module. Here's github copilot's summary:
So it doesn't look like it is possible to do my project in micropython at this time. :-( |
Beta Was this translation helpful? Give feedback.
-
|
I was taking a look at this project and found the following solution to work for my testing purposes. The LED assignments are just so that I can visually see what the current HID device is. Basically we keep the current state of the HID device, then swap to the other one if needed which seems to work well enough in this example. # Reference Material
# https://github.com/orgs/micropython/discussions/17475
# https://github.com/micropython/micropython-lib/blob/fbf7e120c6830d8d04097309e715bcab63dcca67/micropython/usb/examples/device/keyboard_example.py
# https://github.com/micropython/micropython-lib/blob/fbf7e120c6830d8d04097309e715bcab63dcca67/micropython/usb/examples/device/mouse_example.py
import usb.device
from usb.device.keyboard import KeyboardInterface, KeyCode, LEDCode
from usb.device.mouse import MouseInterface
import time
from machine import Pin
# Debugging Status Indication
mouseLED = Pin(15, Pin.OUT) # BLUE
keyboardLED = Pin(8, Pin.OUT) # GREEN
# Default LED States
mouseLED.on()
keyboardLED.off()
lastDevice = 0 # 0-NONE, 1-KEYBOARD, 2-MOUSE
defaultDelay = 150
print("Initializing")
for i in range(10):
print(i)
mouseLED.toggle()
keyboardLED.toggle()
time.sleep_ms(500)
# Default LED States
mouseLED.off()
keyboardLED.off()
print("Device Setup")
kb = KeyboardInterface()
mouse = MouseInterface()
def mouse_init():
# Initialize Mouse If Needed
global lastDevice
if lastDevice != 2:
usb.device.get().init(mouse, builtin_driver=True)
while not mouse.is_open():
time.sleep_ms(100)
time.sleep_ms(250)
lastDevice = 2
keyboardLED.off()
mouseLED.on()
def kb_init():
# Initialize Mouse If Needed
global lastDevice
if lastDevice != 1:
usb.device.get().init(kb, builtin_driver=True)
while not kb.is_open():
time.sleep_ms(100)
time.sleep_ms(250)
lastDevice = 1
mouseLED.off()
keyboardLED.on()
def mouse_left_click(leftRight=1, hold=0):
mouse_init()
if leftRight == 0:
mouse.click_left(False)
mouse.click_right(False)
elif leftRight == 1:
mouse.click_left(True)
time.sleep_ms(100)
if hold == 0:
mouse.click_left(False)
elif leftRight == 2:
mouse.click_right(True)
time.sleep_ms(100)
if hold == 0:
mouse.click_right(False)
time.sleep_ms(defaultDelay)
def mouse_move(posX, posY):
mouse_init()
mouse.move_by(posX, posY)
time.sleep_ms(defaultDelay)
def send_key(keyVal):
kb_init()
kb.send_keys([keyVal])
kb.send_keys([])
time.sleep_ms(defaultDelay)
mouse_move(-100,0)
mouse_move(0,100)
mouse_move(100,0)
mouse_move(0,-100)
send_key(KeyCode.A)
send_key(KeyCode.B)
send_key(KeyCode.C)
send_key(KeyCode.D)
mouse_move(-100,0)
mouse_move(0,100)
mouse_move(100,0)
mouse_move(0,-100)
send_key(KeyCode.A)
send_key(KeyCode.B)
send_key(KeyCode.C)
send_key(KeyCode.D) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm working on writing a micropython program for the the Pico W to make it into a websocket controlled USB-keyboard and mouse. I'm using the the micropython-lib packages
usb-device-keyboardandusb-device-mouse. The keyboard part now seem to be working and I wanted to add mouse support as well. But when initializing the mouse, the keyboard part stops working.Here is how to reproduce the problem.
So my question is, how can I initialize both the keyboard AND the mouse?
My version of micropython is:
Beta Was this translation helpful? Give feedback.
All reactions