Skip to content

Commit

Permalink
Use KBDHID_TypeKeySeq to send EJECT
Browse files Browse the repository at this point in the history
  • Loading branch information
dangfan committed May 12, 2024
1 parent 5bfd94f commit 7ce1860
Showing 1 changed file with 31 additions and 17 deletions.
48 changes: 31 additions & 17 deletions interfaces/USB/class/kbdhid/kbdhid.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <usb_device.h>
#include <usbd_kbdhid.h>

#define EJECT_KEY 0x03

static enum {
KBDHID_Idle,
KBDHID_Typing,
Expand Down Expand Up @@ -82,27 +84,40 @@ static void KBDHID_TypeKeySeq(void) {
DBG_MSG("Key typing ended\n");
state = KBDHID_Idle;
} else if (USBD_KBDHID_IsIdle()) {
uint8_t keycode = ascii2keycode(key_sequence[key_seq_position]);
if (keycode & 0x80) { // Check for shift flag
report.modifier = 0x02; // Shift key
keycode &= 0x7F; // Clear shift flag
if (key_sequence[key_seq_position] == EJECT_KEY) {
report.id = 2;
report.modifier = 0xB8;
// Emulate the key press
USBD_KBDHID_SendReport(&usb_device, (uint8_t *)&report, 2);
} else {
report.modifier = 0; // No modifier key
uint8_t keycode = ascii2keycode(key_sequence[key_seq_position]);
if (keycode & 0x80) { // Check for shift flag
report.modifier = 0x02; // Shift key
keycode &= 0x7F; // Clear shift flag
} else {
report.modifier = 0; // No modifier key
}
report.keycode[0] = keycode;
report.id = 1;
// Emulate the key press
USBD_KBDHID_SendReport(&usb_device, (uint8_t *) &report, sizeof(report));
}
report.keycode[0] = keycode;
report.id = 1;
// Emulate the key press
USBD_KBDHID_SendReport(&usb_device, (uint8_t *)&report, sizeof(report));
state = KBDHID_KeyDown;
}
break;

case KBDHID_KeyDown:
if (USBD_KBDHID_IsIdle()) {
memset(&report, 0, sizeof(report)); // Clear the report
report.id = 1;
// Emulate the key release
USBD_KBDHID_SendReport(&usb_device, (uint8_t *)&report, sizeof(report));
if (key_sequence[key_seq_position] == EJECT_KEY) {
report.id = 2;
// Emulate the key release
USBD_KBDHID_SendReport(&usb_device, (uint8_t *)&report, 2);
} else {
report.id = 1;
// Emulate the key release
USBD_KBDHID_SendReport(&usb_device, (uint8_t *) &report, sizeof(report));
}
key_seq_position++;
state = KBDHID_KeyUp;
break;
Expand All @@ -111,11 +126,10 @@ static void KBDHID_TypeKeySeq(void) {
}

void KBDHID_Eject() {
report.id = 2;
report.modifier = 0xB8;
USBD_KBDHID_SendReport(&usb_device, (uint8_t *)&report, 2);
report.modifier = 0x00;
USBD_KBDHID_SendReport(&usb_device, (uint8_t *)&report, 2);
key_sequence[0] = EJECT_KEY;
key_sequence[1] = 0;
key_seq_position = 0;
state = KBDHID_Typing;
}

uint8_t KBDHID_Init() {
Expand Down

0 comments on commit 7ce1860

Please sign in to comment.