micropython / micropython/micropython-lib

HID keyboard won't release keys when called via schedule

Open
#873 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
2.9k
Forks
1.1k
Avg merge
7d 6h
Merged PRs (30d)
3

Description

Calling KeyboardInterface().send_keys() from a function called via micropython.schedule() doesn't seem to allow keys to be released, causing a spamming issue. This code reproduces the issue:

import usb.device
from usb.device.keyboard import KeyboardInterface
from time import sleep_ms
from micropython import schedule

KBD = None

def send_keys(foo):
    keys = [-0x02, 5]
    print("sending keypress")
    KBD.send_keys(keys)

    keys.clear()
    print("clearing keypress")
    KBD.send_keys(keys)


def main():
    global KBD
    KBD = KeyboardInterface()
    usb.device.get().init(KBD, builtin_driver=True)

    sleep_ms(5000)
    print("starting loop")

    while True:
        send_keys(None)
        # schedule(send_keys, None)
        sleep_ms(3000)

if __name__ == '__main__': main()

running schedule(send_keys, None) instead of send_keys(None) causes a flood of 🅱️, this is on a Pico running 1.23.0 and whatever the latest version of usb-device-keyboard is currently in mip.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with KeyboardInterface.send_keys and compare its behavior when called directly versus through micropython.schedule using the reproduction shown in the issue. Confirm the cause on a Pico with MicroPython 1.23.0, then verify that scheduled key presses release correctly without producing repeated input.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
embedded-iot
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.