HID Keyboard Report out from python (e.g. CAPS LOCK Status)

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
seemefirst@gamil.com
Posts: 4
Joined: Mon Jul 08, 2019 9:15 pm

HID Keyboard Report out from python (e.g. CAPS LOCK Status)

Post by seemefirst@gamil.com » Tue Jul 09, 2019 7:49 pm

Hello,
Just wondering if there is any method to get HID keyboard report out byte in python code.

Reason for asking:
when a py board connected to a host computer and acts as a HID keyboard if you have another keyboard connected to the same host and press "CAPS LOCK" then capitalization on the characters you send out from pyboard will change. For example if you intend to send 'A' you will receive 'a' and vise versa.

Also HID keyboard report out could be used to check the status of "SCROLL LOCK" and "TAB" which add many useful functionalities to switch between USB modes without the user button. Users can store the last mode in a non volatile memory and switch it by pressing any of keys available in report out byte.

Looking forward to have your input.

User avatar
jimmo
Posts: 410
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia

Re: HID Keyboard Report out from python (e.g. CAPS LOCK Status)

Post by jimmo » Wed Jul 10, 2019 11:17 am

Using the recv method on the USB_HID class.

Code: Select all

import pyb
kb = pyb.USB_HID()

buf = bytearray(1)
while True:
    if kb.recv(buf, timeout=20) > 0:
        print(buf)
I don't have a capslock key on my real keyboard but I tested this using `xdotool key Caps_Lock` and I saw the capslock status being reported on a Pyboard using the above code.

seemefirst@gamil.com
Posts: 4
Joined: Mon Jul 08, 2019 9:15 pm

Re: HID Keyboard Report out from python (e.g. CAPS LOCK Status)

Post by seemefirst@gamil.com » Wed Jul 10, 2019 4:00 pm

Thanks Jimmo,

Post Reply