1 year, 8 months ago.

question about keyboard

Hi, I want to use mbed to emulate USB keyboard in order to play game. How about comparing with normal keyboard?

2 Answers

1 year, 8 months ago.

Hi Hamy,

The advantage to use USBkeyboard is that you can program a sequence of designed instructions. If you would like to play game like normal joy con or keyboard, I think buy a good keyboard would be better :D.

Regards, Desmond

1 year, 8 months ago.

In addition to the USBKeyboard that Desmond mentioned there is also a USBMouseKeyboard which allows you to make the mbed look like both a mouse and a keyboard.

Generally a PC will allow two different keyboards to be used at the same time so you can have both the mbed and a normal keyboard plugged in.

So if you have a particularly long or time critical mouse movement or key sequence you want to do repeatedly then you can configure the mbed to perform this and then use a normal keyboard for everything else. The simple way to trigger this would be a button on the mbed but there is one other trick you can use. If you have two keyboards plugged in then the num lock, scroll lock and caps lock LEDs on them are common between both keyboards. Press caps lock on one keyboard and the light comes on on both. This means that your mbed pretending to be a keyboard will be notified when you press caps lock on your normal keyboard, this gives you a method to trigger the mbed without a physical button connected to it. Turning caps lock on and off in your game may have an impact on the controls and so probably isn't a good choice. But I've not seen any modern game that cares about the scroll lock status so that key is certainly available to signal things and for most games num lock is also usable.

Just one more thing to mention to what Andy explained is that the Mbed UBSKeyboard and USBMouseKeyboard libraries are emulating a full key press (key release included). That could be a problem when using a "Mbed keybord" with games or other PC software. However, as already pointed out by Stefan Pasteiner, even in such situations it is possible to do what you want.
For example, when building an additional parallel keyboard (as explained by Andy) with just few keys (arrow keys + up + down +shift) to control a PC software, I realized that key combinations like Shift + Left_arrow, Shift + Up ... etc. do not work. A solution was to remove (comment out) "key release" from Mbed's USBKeyboard library:


bool USBKeyboard::keyCode(uint8_t key, uint8_t modifier) {
    // Send a simulated keyboard keypress. Returns true if successful.
    HID_REPORT report;

    report.data[0] = REPORT_ID_KEYBOARD;
    report.data[1] = modifier;
    report.data[2] = 0;
    report.data[3] = keymap[key].usage;
    report.data[4] = 0;
    report.data[5] = 0;
    report.data[6] = 0;
    report.data[7] = 0;
    report.data[8] = 0;

    report.length = 9;

    if (!send(&report)) {
        return false;

//    report.data[1] = 0;
//    report.data[3] = 0;

//    if (!send(&report)) {
//        return false;
//    }

    return true;


and rather do it in the user software. For example as:


    USBKeyboard    keyboard;
int main()
    keyboard.keyCode(LEFT_ARROW, KEY_SHIFT);  // Shift + Left_arrow
    keyboard.keyCode(0, 0);  // release keys

It's worth to have a look also at Wim's USB Joystick and Mark's Game pad.

posted by Zoltan Hudak 26 Nov 2019