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¶
#include "mbed.h" #include "USBKeyboard.h" USBKeyboard keyboard; int main(void) { while (1) { keyboard.mediaControl(KEY_VOLUME_DOWN); keyboard.printf("Hello World from Mbed\r\n"); wait(1); } }
Import programUSBKeyboard_HelloWorld
USBKeyboard Hello World
Currently only available in betamode!
This library is in beta, and only works with the betamode compiler and the beta version of the libraries.
To use these examples, ensure you have enabled /betamode for the compiler, and then import these examples as the basis for your experiments to ensure the beta mbed library is pulled in.
API¶
[Not converted]
More examples¶
Program which controls sound and tracks of your playlist with switches:
#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); } }
Import programUSBKeyboard_mediaKeys
USBKeyboard example with media keys