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

Hello World

» Import this program

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

API

» Import this library into a program

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



4 related questions:

27 comments:

03 Nov 2011

I am very happy that new examples of USB hid devices examples are here at mbed community.

If I can ask I vote for USB Joystick and USB Mass storage (my idea is that mbed will act as USB mass storage device for host with /local or /SD card filesystem mounted as storage space). I found example of USB Mass storage at keil.com but there was used any virtual file system at flash or so as I remember. But porting for mbed was far away my skills. And why USB Mass storage class? You easily plug mbed via D+, D-, Vin, Gnd to host pc as USB mass storage, load files to /sd card attached filesystem as storage space, unplug mbed, restart and mbed can work with new data. Now You can already connect mbed with its mini USB connector but it mounts only /local filesystem so You are limited with 2mb capacity. Yes You can insert an SD card to pc itself but with USB mass storage class device we can use mbed as something like USB SD card reader with features of USB mass storage device.

Now back to USB Mouse, keyboard and common hid device. As I can see there are send, read and readNB "functions". I also added send report to code I work with. Reason is simple. If you want to send to mouse movement and scroll in one report, using mouse.move(10,0); mouse.scroll(1); it results to sending mouse movement in on report and scroll in second report. So I am very happy for send, read and readNB, thumb up!!!

Please if anybody interested in /sd card filesystem mounted to mbed acting as USB mass storage device, leave reply.

03 Nov 2011

Hi Little,

Great to hear you like it. More interfaces are coming soon!

And note for USBMouse, you can use the update() method to send the co-ordinate, button and scroll wheel information in one go if you want!

Simon

03 Nov 2011

These are great news for me, because I am very interested in USB. The next level of USB devices can be USB host with BT dongle acting as bluetooth HID. Now I am trying these new examples. Thank You very much.

05 Nov 2011

YIPPY BASIC HID

I have been dying to get this working on my MBED.

I can final start migrating my HID designs. On a similar note, what about Number Lock, Scroll Lock Function,.as well as NUMLOCK SCROLLLOCK LEDS.

Cheers, a very happy MBED'er

Ceri :)

15 Nov 2011

Hi Ceri,

Feature added. You can now call the function lockStatus() to read the state of lock keys. As you can see in the Hello World example, I control leds according to lock keys. The first bit of the result represents NUM_LOCK, the second CAPS_LOCK and the last one SCROLL_LOCK.

Don't hesitate to share your HID designs. That sounds very cool.

Sam

16 Nov 2011

How can i get 2, 3, or even four HID's to run in one MBED ? Simmilar to Keyboard & Mouse !

Cheers

Ceri

29 Nov 2011

Trying to run this on the LPC1768 but not having any luck. Changed out the M0 Library and put in the Mbed library and it compiles just fine but I just get an unrecognised device in windows? Any ideas

29 Nov 2011

Hi Steven,

Make sure that your hardware part is good. D- on D- and D+ on D+.

Can you try another library (USBHID, USBSerial, USBMouse,...) to see if you have the same problem ?

Which version of Windows are you using ?

Cheers

30 Nov 2011

Its alright I got it in the end. D+ and D- were the wrong way round despite checking the cable wire colours multiple times.

My only other problem is that my KVM doesn't recognise it. Windows installs the general keyboard driver just fine when connected directly but it seems to lock up my kvm.

I was trying to use it to switch the output on my KVM which requires the scroll lock key to be pressed twice and I have confirmed that when plugged into windows the key is pressed twice but it seems to hang the mbed before entering main (presumable inside the instantiation of the keyboard)

Any ideas? my kvm is a Aten CS-62U

08 Jan 2012

For anyone who wanted to know the windows modifier key is 8 so keyboard.keyCode('l', 8); would lock a windows PC

29 Jan 2012

This is a neat library. I've been tying this together with a wii nunchuck to make a smart mouse/controller. One thing I can't work out how to do is to send the signal for direction (up/down/left/right) keys in order to send a command to switch desktops areas. Is it possible to send these signals?

30 Jan 2012

Hi Nick,

I have just updated the USB library. You can access down/up/left/right arrows by using:

    keyboard.keyCode(DOWN_ARROW);
    keyboard.keyCode(UP_ARROW);
    keyboard.keyCode(LEFT_ARROW);
    keyboard.keyCode(RIGHT_ARROW);

Sam

15 Apr 2012

Hi Sam,

How difficult would it be to read/control key press/depress information? I'm using the library to emulate a nintendo controller over USB, and in this context there is a difference between 'send A repeatedly' and 'hold down the A button'.

Cheers, Craig

16 Apr 2012

Hi Craig,

You can see in the USBDevice library/USBHID/USBKeyboard how I send a press/depress information (in the keyCode() method for instance). In fact two different reports are sent.

Hope that helps

Sam

13 Jun 2012

Can you describe the type of variables needed for scroll and Key_F's?

these don't work when i try them: key_mouse.scroll(1); and key_mouse.scroll(-1); key_mouse.keycode(Key_F1); all the way to F12

Thanks :)

13 Jun 2012

Hi Matthew,

Which OS are you using?

- Try to put scroll(10) and be sure that you have a content that can be scrolled. - I just tested keyCode(KEY_F11) and my screen switched to full screen. Have you tested KEY_F11 ?

Sam

14 Jun 2012

user Samuel Mokrani wrote:

Hi Matthew,

Which OS are you using?

- Try to put scroll(10) and be sure that you have a content that can be scrolled. - I just tested keyCode(KEY_F11) and my screen switched to full screen. Have you tested KEY_F11 ?

Sam

I will try that now one moment

14 Jun 2012

Awesome, they're all working now, idk what the problem was! >.< but now that i've re written the code they seem to work perfectly fine!

01 Oct 2012

Please tell me how to use the ESC key.

04 Oct 2012

I want to send the character ñ, how can i do this?

05 Oct 2012

Hi,

@naoki kimura: as a temporary solution, you can do a small patch to USBKeyboard to send the ESC key:

  • modify in USBKeyboard.cpp (line 63)

    {0, 0},             /* ESC */

to

    {0x29, 0},             /* ESC */
  • define in USBKeyboard.h a new symbol for the ESCAPE key:

#define ESCAPE 0x1b
  • You can then invoke in your main code:

keyboard.keyCode(ESCAPE);

@alteio sdc: You can try to send unicode characters.

  • On linux, unicode characters can be sent by pressing CTRL+SHIFT+U and then the hex code of the character (list) (f1 for ñ) followed by "\r\n":

keyboard.keyCode('U', KEY_CTRL | KEY_SHIFT);
keyboard.printf("f1\r\n");
  • On Windows, there is another specific sequence to enter these characters: press and hold ALT followed by the code of the character (code). The problem is that the ALT key has to be held. The current API does not allow this capability. A patch is coming soon ;)

Cheers, Sam

27 Dec 2012

Hi, Been testing with the USBkeyboard library and it on the LPC11U24 and it is recognized by the host PC fine as HID device. But I want to alter it into a true keyboard so it needs to send 'key down' and 'key release' reports. I notice in your function you send a report with the keymap code and the modifier bits first and then a report with both of them zeroed out. Is this a general 'release all keys' report ? My goal is to send the raw keycode reports for the normal keys and CTRL, ALT and Shift (both left and right) so combinations of keys can also be sent (like Ctrl-Alt-Delete and such). How would I do this ?

02 Feb 2013

output of pc.printf() can be seen on terminal. But where to see the output of the statements keyboard.printf("Hello World from Mbed\r\n"); keyboard.keyCode('s', KEY_SHIFT);

02 Feb 2013

Hi Pravin,

The messages should appear somewhere where you can actually tap keys with your keyboard (text editor / ....). This library emulates a keyboard.

Cheers, Sam

05 Feb 2013

I imported the program USBKeyboard_HelloWorld.cpp and compiled it online and saved USBKeyboard_HelloWorld.bin to the memory of LPC11U24. Before pressing the reset button of the device I opened Notepad. After that I pressed the reset button of the device but still I don't see any output. What may be the problem? i AM ONLY USING THE DEVICE WITHOUT BREADBOARD. IS THAT A PROBLEM?

06 Feb 2013

Hi Pravin-

USBKeyboard uses the USB connection on pins 31 and 32 and not the built in USB connector on top of the mBed.

Mike

user Pravin Phule wrote:

I imported the program USBKeyboard_HelloWorld.cpp and compiled it online and saved USBKeyboard_HelloWorld.bin to the memory of LPC11U24. Before pressing the reset button of the device I opened Notepad. After that I pressed the reset button of the device but still I don't see any output. What may be the problem? i AM ONLY USING THE DEVICE WITHOUT BREADBOARD. IS THAT A PROBLEM?

18 Mar 2013

user Gert van der Knokke wrote:

Hi, Been testing with the USBkeyboard library and it on the LPC11U24 and it is recognized by the host PC fine as HID device. But I want to alter it into a true keyboard so it needs to send 'key down' and 'key release' reports. I notice in your function you send a report with the keymap code and the modifier bits first and then a report with both of them zeroed out. Is this a general 'release all keys' report ? My goal is to send the raw keycode reports for the normal keys and CTRL, ALT and Shift (both left and right) so combinations of keys can also be sent (like Ctrl-Alt-Delete and such). How would I do this ?

Hi

I too would really like to be able to do this.

I want to use it as a keyboard encoder for video game emulation.

Any progress on detecting the key down and key up events?

Posting comments for this page has been disabled