USBKeyboard
This content relates to a deprecated version of Mbed
Mbed 2 is now deprecated. For the latest version please see the Mbed OS documentation.
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¶
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);
}
}