How to use STM32 as a KEYBOARD

I have already covered the tutorial on emulating the BluePill (F103C8) as a mouse. This tutorial will cover another HID example, and today we will see how to use STM32F103C8 as a keyboard for the computer.

Well some part of this tutorial will be identical to the mouse one, but we need to modify the rest of it. so let’s start by setting up the CubeMX.

I am creating this project in CubeIDE, and the following is the setup required in CubeMX

CubeMX Setup

USB SETUP

USB setup

In the picture above, I have selected the USB Device HID class. Everything else is kept default. That’s basically all for the Cube setup. Generate the project now


We need to modify some pre generated library files. So go to Middlewares -> ST -> STM32_USB_Device_Library -> Class -> HID -> src and open usbd_hid.c file as shown below

In usbd_hid.c file, locate the function USBD_HID_CfgFSDesc. Here we need to change the Interface Protocol from mouse to keyboard. The CubeMX always generate the protocol for mouse (0x02) by default, and to use it as a keyboard (0x01) , we need to change this to 0x01 as shown below

Now go down to the function HID_MOUSE_ReportDesc. It have the report descriptor for mouse by default, and we need to replace them with the keyboard descriptor. You can download the Keyboard Descriptor from HERE. Check the image below

Locate the HID_MOUSE_REPORT_DESC_SIZE. It must be 74 by default, for the mouse descriptor. Change it to 63 (Keyboard descriptor size).

This completes the setup part for this tutorial. Now let’s see the code now






Some insight into the CODE

First of all I am going to create a structure to store the values for the keyboard.

typedef struct
{
	uint8_t MODIFIER;
	uint8_t RESERVED;
	uint8_t KEYCODE1;
	uint8_t KEYCODE2;
	uint8_t KEYCODE3;
	uint8_t KEYCODE4;
	uint8_t KEYCODE5;
	uint8_t KEYCODE6;
} keyboardHID;

keyboardHID keyboardhid = {0,0,0,0,0,0,0,0};

The above structure is as per the protocol used in keyboard. The picture for the protocol is shown below. You can download the PDF from HERE


Now let’s say I want to send the ‘AB’ to the computer, i will the use code mentioned below

keyboardhid.MODIFIER = 0x02;  // left Shift
keyboardhid.KEYCODE1 = 0x04;  // press 'a'
keyboardhid.KEYCODE2 = 0x05;  // press 'b'
USBD_HID_SendReport(&hUsbDeviceFS, &keyboardhid, sizeof (keyboardhid));
HAL_Delay (50);

keyboardhid.MODIFIER = 0x00;  // shift release
keyboardhid.KEYCODE1 = 0x00;  // release key
keyboardhid.KEYCODE2 = 0x00;  // release key
USBD_HID_SendReport(&hUsbDeviceFS, &keyboardhid, sizeof (keyboardhid));
HAL_Delay (1000);
  • First press the left shift Modifier
  • then press ‘A‘ and ‘B
  • send the report
  • release the Modifier, release ‘A’ and ‘B‘ also
  • send the report again
  • we can send 6 Key Codes at once, so it’s like pressing 6 keys at the same time. Although I have only used 2 keys here

The keycode for each key in mentioned in the PDF HERE




Result

You can see above that the MCU is being detected as STM32 Keyboard, and also the ‘AB‘ is printing every 1 second

Check out the Video Below










Info

You can help with the development by DONATING
To download the code, click DOWNLOAD button and view the Ad. The project will download after the Ad is finished.

5 Comments. Leave new

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

keyboard_arrow_up

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add controllerstech.com to your ad blocking whitelist or disable your adblocking software.

×