Is it a ProMicro?
You have a few options:
Load the bootloader via ICSP
Disconnect the ProMicro.
Open blink or other innocent sketch.
Connect the ProMicro while keeping it in reset. You will need to wire it or use a button between reset pin and GND.
Upload the sketch; when the IDE reports the memory usage, release the reset button / remove the wire.
It's important to keep the ProMicro in reset so it does not press the key till you the IDE reports the memory usage.
And next add a safety measure so you can stop the spamming
const byte safetyPin = A0;
if (digitalRead(safetyPin) == LOW)
// do HID stuff here
Above uses A0 but you can use any pin for the safetyPin. The above will only send HID stuff if the pin is shorted to ground; you can change LOW to HIGH and it will only send HID stuff if the pin is not shorted to ground.