How to emulate a usb keyboard

I thought this was going to be easy, but I seem to be struggling with the basics. I want to use the keyboard.printlnf command to send keystrokes to an open notepad on my PC - like a simple console. I have my photon connected to my PC over USB and have loaded this code for testing. I can see by the color change on the status LED when I have hit the input, but the keypoard.print command doesn’t seem to do anything. :

int button = D7;
STARTUP(Keyboard.begin());

void setup() {
    pinMode(button, INPUT_PULLDOWN);
    delay(2000);
}

void loop() {
    if(digitalRead(button) == HIGH){
        RGB.control(true);
        RGB.brightness(64);
        RGB.color(255, 0, 0);  //red
        Keyboard.printlnf("Test", millis());
        delay(1000);
        RGB.control(false);
    }
    while(digitalRead(button) == HIGH) {
        delay(500);
    }
}

I have loaded the Photon 1.4.4 firmware and I am running Windows10 on my PC . What am I doing wrong?

I have tried this slightly simplified code and that works for me just fine with 1.4.4 on Win10

(I’m using the SETUP button instead of an external button on D7)

STARTUP(Keyboard.begin());

void loop() {
  if(!digitalRead(BTN)) {
    RGB.control(true);
    RGB.brightness(64);
    RGB.color(255, 0, 0);  //red
    Keyboard.printlnf("Test %d", millis());
    while(!digitalRead(BTN));
    RGB.control(false);
  }
}
1 Like

Thanks!! It turned out the code worked all along, I apparently have a stack of power only USB cables. I finally found one with data as well and it is working great now.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.