HID: Only works after upload

Hi,

I need my Arduino Pro Micro to send a "space" key to the PC with HID. It works fine, but only after upload. When I plug in my Pro Micro and toggle the button, nothing happens. But when I upload the sketch to the board, then toggle the button, the pro Micro sends the space key.

Where is my mistake?

thanks a lot
Bests
Jan

#include <Keyboard.h>
#include <HID.h>
int inPin = 8;
int val = 0;
  
void setup() {
  pinMode(inPin, INPUT);   
  Keyboard.begin();   
}

void loop() {
 

  if(digitalRead(inPin) == HIGH && val == 0)
  {
     Keyboard.write(32);
     val = 1;   
  }

}

Do you have a pull-down resistor on your button?

I think a reset will have the same effect as an upload.

Your val variable is initialised with 0.

The first time you press the button, the if condition evaluates to true and val be be set 1 and after that the if condition will never evaluate to true so no space will be send.

Thanks. Yes, there is a pull-down.

sterretje:
Your val variable is initialised with 0.

The first time you press the button, the if condition evaluates to true and val be be set 1 and after that the if condition will never evaluate to true so no space will be send.

True. But even when I reset the arduino or if I reconnect the Arduino to the power, it does not work! Only if I upload the sketch, it works after the upload.

Is there a problem with the serial connection? That the connection is only established when/after uploading, not if I plug in the Arduino without uploading the skecth? Seems so?

No idea. If nobody here can help you, maybe ask Sparkfun, the pro micro is their product.

I only have a Leonardo do can't really advise further.