Skip to content

Commit

Permalink
wip: fix modifer in HID
Browse files Browse the repository at this point in the history
  • Loading branch information
dangfan committed Jan 12, 2024
1 parent a9c2b76 commit 06ee655
Showing 1 changed file with 37 additions and 25 deletions.
62 changes: 37 additions & 25 deletions interfaces/USB/class/kbdhid/kbdhid.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,51 +18,56 @@ static uint8_t key_seq_position;
static keyboard_report_t report;

static uint8_t ascii2keycode(char ch) {
// digits and letters
const uint8_t shift = 0x80; // Shift key flag

// digits and lowercase letters
if ('1' <= ch && ch <= '9')
return 30 + ch - '1';
if ('0' == ch)
return 39;
if ('a' <= ch && ch <= 'z')
return 4 + ch - 'a';

// uppercase letters
if ('A' <= ch && ch <= 'Z')
return 4 + ch - 'A';
return (4 + ch - 'A') | shift;

// symbols and special characters
switch(ch) {
case 13: return 0x28; // \r
case 32: return 0x2C; // space
case 33: return 0x1E; // !
case 34: return 0x34; // "
case 35: return 0x20; // #
case 36: return 0x21; // $
case 37: return 0x22; // %
case 38: return 0x24; // &
case 33: return 0x1E | shift; // !
case 34: return 0x34 | shift; // "
case 35: return 0x20 | shift; // #
case 36: return 0x21 | shift; // $
case 37: return 0x22 | shift; // %
case 38: return 0x24 | shift; // &
case 39: return 0x34; // '
case 40: return 0x26; // (
case 41: return 0x27; // )
case 42: return 0x25; // *
case 43: return 0x2E; // +
case 40: return 0x26 | shift; // (
case 41: return 0x27 | shift; // )
case 42: return 0x25 | shift; // *
case 43: return 0x2E | shift; // +
case 44: return 0x36; // ,
case 45: return 0x2D; // -
case 46: return 0x37; // .
case 47: return 0x38; // /
case 58: return 0x33; // :
case 58: return 0x33 | shift; // :
case 59: return 0x33; // ;
case 60: return 0x36; // <
case 60: return 0x36 | shift; // <
case 61: return 0x2E; // =
case 62: return 0x37; // >
case 63: return 0x38; // ?
case 64: return 0x1F; // @
case 62: return 0x37 | shift; // >
case 63: return 0x38 | shift; // ?
case 64: return 0x1F | shift; // @
case 91: return 0x2F; // [
case 92: return 0x31; // "\"
case 93: return 0x30; // ]
case 94: return 0x23; // ^
case 95: return 0x2D; // _
case 94: return 0x23 | shift; // ^
case 95: return 0x2D | shift; // _
case 96: return 0x35; // `
case 123: return 0x2F; // {
case 124: return 0x31; // |
case 125: return 0x30; // }
case 126: return 0x35; // ~
case 123: return 0x2F | shift; // {
case 124: return 0x31 | shift; // |
case 125: return 0x30 | shift; // }
case 126: return 0x35 | shift; // ~
default: return 0; // undefined
}
}
Expand All @@ -77,7 +82,14 @@ static void KBDHID_TypeKeySeq(void) {
DBG_MSG("Key typing ended\n");
state = KBDHID_Idle;
} else if (USBD_KBDHID_IsIdle()) {
report.keycode[0] = ascii2keycode(key_sequence[key_seq_position]);
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;
// Emulate the key press
USBD_KBDHID_SendReport(&usb_device, (uint8_t *)&report, sizeof(report));
state = KBDHID_KeyDown;
Expand All @@ -86,7 +98,7 @@ static void KBDHID_TypeKeySeq(void) {

case KBDHID_KeyDown:
if (USBD_KBDHID_IsIdle()) {
report.keycode[0] = 0;
memset(&report, 0, sizeof(report)); // Clear the report
// Emulate the key release
USBD_KBDHID_SendReport(&usb_device, (uint8_t *)&report, sizeof(report));
key_seq_position++;
Expand Down

0 comments on commit 06ee655

Please sign in to comment.