Skip to content

Instantly share code, notes, and snippets.

@NaokiStark
Created April 23, 2021 03:36
Show Gist options
  • Save NaokiStark/5fe7887aba4609b5129119a4862c2a3f to your computer and use it in GitHub Desktop.
Save NaokiStark/5fe7887aba4609b5129119a4862c2a3f to your computer and use it in GitHub Desktop.
From https://github.com/onetransistor/MediaKeyboard [modified for simultaneous keys and release for stm32]
#include "MediaKeyboard.h"
#if defined(USBCON)
#include "usbd_hid_consumer_if.h"
typedef struct
{
uint8_t MODIFIER;
uint8_t RESERVED;
uint8_t KEY1; //X
uint8_t KEY2; //Z
uint8_t KEY3;
uint8_t KEY4;
uint8_t KEY5;
uint8_t KEY6;
} keyboardReport;
keyboardReport kbreport = {0, 0, 0, 0, 0, 0, 0, 0};
MediaKeyboard_::MediaKeyboard_(void)
{
}
void MediaKeyboard_::begin(void)
{
HID_Composite_Init(HID_CONSUMER);
}
void MediaKeyboard_::end(void)
{
HID_Composite_DeInit(HID_CONSUMER);
}
void MediaKeyboard_::press(uint8_t mediaKey)
{
if (mediaKey == KEY_X)
{
kbreport.KEY1 = KEY_X;
HID_Composite_keyboard_sendReport((uint8_t *)&kbreport, sizeof(kbreport));
}
else if (mediaKey == KEY_Z)
{
kbreport.KEY2 = KEY_Z;
HID_Composite_keyboard_sendReport((uint8_t *)&kbreport, sizeof(kbreport));
}
}
void MediaKeyboard_::release(uint8_t mediaKey)
{
if (mediaKey == KEY_X)
{
kbreport.KEY1 = 0x00;
HID_Composite_keyboard_sendReport((uint8_t *)&kbreport, sizeof(kbreport));
}
else if (mediaKey == KEY_Z)
{
kbreport.KEY2 = 0x00;
HID_Composite_keyboard_sendReport((uint8_t *)&kbreport, sizeof(kbreport));
}
}
MediaKeyboard_ MediaKeyboard;
#endif
#ifndef MEDIAKEYBOARD_H
#define MEDIAKEYBOARD_H
#include <Arduino.h>
#if !defined(USBCON) || !defined(USBD_USE_HID_COMPOSITE)
#error "USB HID not enabled! Select 'HID' in the 'Tools->USB interface' menu."
#else
#define VOLUME_UP 0xE9
#define VOLUME_DOWN 0xEA
#define VOLUME_MUTE 0xE2
#define MEDIA_PLAY_PAUSE 0xCD
#define MEDIA_STOP 0xB7
#define MEDIA_NEXT 0xB5
#define MEDIA_PREV 0xB6
#define KEY_X 0x1B
#define KEY_Z 0x1D
class MediaKeyboard_
{
private:
public:
MediaKeyboard_(void);
void begin(void);
void end(void);
void press(uint8_t mediaKey);
void release(uint8_t mediaKey);
};
extern MediaKeyboard_ MediaKeyboard;
#endif
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment