USBKeyboard

mbed OS 2 and mbed OS 5

This is the handbook for mbed OS 2. If you’re working with mbed OS 5, please see the new handbook and API References.

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 for the LPC1768 and the LPC11U24
  • The on-board USB connector of the FRDM-KL25Z

Hello World

Import program

00001 #include "mbed.h"
00002 #include "USBKeyboard.h"
00003  
00004 //LED1: NUM_LOCK
00005 //LED2: CAPS_LOCK
00006 //LED3: SCROLL_LOCK
00007 BusOut leds(LED1, LED2, LED3);
00008  
00009 //USBKeyboard
00010 USBKeyboard keyboard;
00011  
00012 int main(void) {
00013     while (1) {
00014         keyboard.mediaControl(KEY_VOLUME_DOWN);
00015         keyboard.printf("Hello World from Mbed\r\n");
00016         keyboard.keyCode('s', KEY_CTRL);
00017         keyboard.keyCode(KEY_CAPS_LOCK);
00018         wait(1);
00019         leds = keyboard.lockStatus();
00020     }
00021 }

API

[Not found]

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);
    }
}