Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of USBDevice by
Diff: USBHID/USBKeyboard.cpp
- Revision:
- 73:803bc631881e
- Parent:
- 72:8c3160323201
diff -r 8c3160323201 -r 803bc631881e USBHID/USBKeyboard.cpp --- a/USBHID/USBKeyboard.cpp Sat Aug 18 06:37:49 2018 +0000 +++ b/USBHID/USBKeyboard.cpp Sat Aug 25 07:54:43 2018 +0000 @@ -183,7 +183,7 @@ {0x4a, 0}, /* HOME */ {0x4b, 0}, /* PAGE_UP */ {0x4e, 0}, /* PAGE_DOWN */ - + {0x4f, 0}, /* RIGHT_ARROW */ {0x50, 0}, /* LEFT_ARROW */ {0x51, 0}, /* DOWN_ARROW */ @@ -344,7 +344,7 @@ {0x4a, 0}, /* HOME */ {0x4b, 0}, /* PAGE_UP */ {0x4e, 0}, /* PAGE_DOWN */ - + {0x4f, 0}, /* RIGHT_ARROW */ {0x50, 0}, /* LEFT_ARROW */ {0x51, 0}, /* DOWN_ARROW */ @@ -355,7 +355,8 @@ /// /// reportDesc /// -uint8_t * USBKeyboard::reportDesc() { +uint8_t * USBKeyboard::reportDesc() +{ static uint8_t reportDescriptor[] = { USAGE_PAGE(1), 0x01, // 標準デスクトップ USAGE(1), 0x06, // キーボード @@ -425,14 +426,15 @@ /// /// EP1_OUT_callback /// -bool USBKeyboard::EP1_OUT_callback() { +bool USBKeyboard::EP1_OUT_callback() +{ uint32_t bytesRead = 0; uint8_t led[65]; USBDevice::readEP(EPINT_OUT, led, &bytesRead, MAX_HID_REPORT_SIZE); - + // led[0]はレポートIDなので、我々はled[1]を取ります。 lock_status = led[1] & 0x07; - + // データを受信できるようにエンドポイントをアクティブ化します。 if (!readStart(EPINT_OUT, MAX_HID_REPORT_SIZE)) return false; @@ -442,21 +444,24 @@ /// /// ロックステータスを返す /// -uint8_t USBKeyboard::lockStatus() { +uint8_t USBKeyboard::lockStatus() +{ return lock_status; } /// /// 文字を送信する /// -int USBKeyboard::_putc(int c) { +int USBKeyboard::_putc(int c) +{ return keyCode(c, keymap[c].modifier); } /// /// キーコード /// -bool USBKeyboard::keyCode(uint8_t key, uint8_t modifier) { +bool USBKeyboard::keyCode(uint8_t key, uint8_t modifier) +{ // シミューレートされたキーボードキーを送信します。成功した場合はtrueを返します。 HID_REPORT report; @@ -487,10 +492,73 @@ } +uint8_t downKey = 0; // ダウン中のキー + +/// +/// キーダウン +/// +bool USBKeyboard::keyDown(uint8_t key) +{ + // シミューレートされたキーボードキーを送信します。成功した場合はtrueを返します。 + HID_REPORT report; + + report.data[0] = REPORT_ID_KEYBOARD; + report.data[1] = keymap[key].modifier; + report.data[2] = 0; + report.data[3] = keymap[key].usage; + report.data[4] = 0; + report.data[5] = 0; + report.data[6] = 0; + report.data[7] = 0; + report.data[8] = 0; + + report.length = 9; + + if (!send(&report)) { + return false; + } + + downKey = key; // ダウンしたキーを保存 + + return true; +} + +/// +/// キーアップ +/// +bool USBKeyboard::keyUp(uint8_t key) +{ + // ダウンしたキーと異なるキーなら終了 + if (downKey != key) return; + + // キーアップをシミュレートします。 + HID_REPORT report; + + report.data[0] = REPORT_ID_KEYBOARD; + report.data[1] = 0; + report.data[2] = 0; + report.data[3] = 0; + report.data[4] = 0; + report.data[5] = 0; + report.data[6] = 0; + report.data[7] = 0; + report.data[8] = 0; + + report.length = 9; + + if (!send(&report)) { + return false; + } + + return true; +} + + /// /// メディアコントロール /// -bool USBKeyboard::mediaControl(MEDIA_KEY key) { +bool USBKeyboard::mediaControl(MEDIA_KEY key) +{ HID_REPORT report; report.data[0] = REPORT_ID_VOLUME; @@ -520,7 +588,8 @@ /// /// Descの設定 /// -uint8_t * USBKeyboard::configurationDesc() { +uint8_t * USBKeyboard::configurationDesc() +{ static uint8_t configurationDescriptor[] = { CONFIGURATION_DESCRIPTOR_LENGTH,// bLength CONFIGURATION_DESCRIPTOR, // bDescriptorType