You are viewing an older revision! See the latest version
USBKeyboard
The USBKeyboard interface is used to emulate a keyboard over the USB port. You can type strings and send keycodes, send keys with modifiers (e.g. CTRL + 's'), function keys and also the media control keys
The USB connector should be attached to p31 (D+), p32 (D-) and GND. You can also connect the USB power to VIN to power the mbed when connected.
Hello World¶
USBKeyboard Hello World
#include "mbed.h" #include "USBKeyboard.h" //LED1: NUM_LOCK //LED2: CAPS_LOCK //LED3: SCROLL_LOCK BusOut leds(LED1, LED2, LED3); //USBKeyboard USBKeyboard keyboard; int main(void) { while (1) { keyboard.mediaControl(KEY_VOLUME_DOWN); keyboard.printf("Hello World from Mbed\r\n"); keyboard.keyCode('s', KEY_CTRL); keyboard.keyCode(KEY_CAPS_LOCK); wait(1); leds = keyboard.lockStatus(); } }
Import programUSBKeyboard_HelloWorld
USBKeyboard Hello World
API¶
Import library
Public Member Functions |
|
USBKeyboard (uint16_t vendor_id=0x1235, uint16_t product_id=0x0050, uint16_t product_release=0x0001) | |
Constructor.
|
|
bool | keyCode (uint8_t key, uint8_t modifier=0) |
To send a character defined by a modifier(CTRL, SHIFT, ALT) and the key.
|
|
virtual int | _putc (int c) |
Send a character.
|
|
bool | mediaControl (MEDIA_KEY key) |
Control media keys.
|
|
uint8_t | lockStatus () |
Read status of lock keys.
|
|
bool | send (HID_REPORT *report) |
Send a Report.
|
|
bool | sendNB (HID_REPORT *report) |
Send a Report.
|
|
bool | read (HID_REPORT *report) |
Read a report: blocking.
|
|
bool | readNB (HID_REPORT *report) |
Read a report: non blocking.
|
More examples¶
Program which controls sound and tracks of your playlist with switches:
USBKeyboard and media keys
#include "mbed.h" #include "USBKeyboard.h" USBKeyboard keyboard; //Bus of buttons BusInOut buttons(p21, p22, p23, p24, p25, p26, p29); int main(void) { uint8_t p_bus = 0; while (1) { //if the bus of buttons has changed, send a report if (buttons.read() != p_bus) { p_bus = buttons.read(); if(p_bus & 0x01) keyboard.mediaControl(KEY_MUTE); if(p_bus & 0x02) keyboard.mediaControl(KEY_VOLUME_DOWN); if(p_bus & 0x04) keyboard.mediaControl(KEY_VOLUME_UP); if(p_bus & 0x08) keyboard.mediaControl(KEY_NEXT_TRACK); if(p_bus & 0x10) keyboard.mediaControl(KEY_PLAY_PAUSE); if(p_bus & 0x20) keyboard.mediaControl(KEY_PREVIOUS_TRACK); if(p_bus & 0x40) keyboard.printf("Hello World\r\n"); } wait(0.01); } }