10 years, 11 months ago.  This question has been closed. Reason: Duplicate of existing question.

Using USBKeyboard with InterruptIn

Hello guys! I'm new in C++ programming, picked it up for school project purposes. So I am to use mbed to emulate USBKeyboard for gaming purposes (yes, I am trying to create something like a joystick/controller for pc gaming). So I wrote the following program with the limited programming knowledge I have.

Currently testing it with push buttons mounted on breadboard. My problem is, once you've pressed a single button, the text would print non-stop, no matter what program you're on (note pad, browser, word document, mbed compiler etc). It works on games like I intended, however the game character would just keep running in one single direction. Pressing other buttons wouldn't change its state, and it only stops when I press the reset button on mbed.

I've connected the pins like the guys did in this video

I'm guessing the problem lies with the interrupt or that the USBKeyboard library isn't compatible with interrupt. Are there any ways to break the interrupt? Please advise, thanks you!

#include "mbed.h"
#include "USBKeyboard.h"
 
USBKeyboard keyboard;
 
InterruptIn a(p23);
InterruptIn d(p24);
 
DigitalOut myled(LED2);
 
void ai ()
{
    keyboard.keyCode('a');
}
 
void di ()
{
    keyboard.keyCode('d');
}
 
int main(void)
{
    a.rise(&ai);
    d.rise(&di);
    
    while (1) {
    myled = !myled;
    }
}

Question relating to: