Post the error.
Doesn't matter. The principles are basically the same.
- Disconnect the Leonardo.
- Reboot your PC; this step might not be needed but if the procedure fails without rebooting the PC, you have to do this.
- Load an innocent sketch (e.g. blink).
- Press the reset button on the Leonardo; don't release.
- Connect the Leonardo keeping the reset button pressed.
- Start the compile / upload; keeping the reset button pressed.
- When the IDE reports the memory usage, release the reset button.
I think that the main problem with your sketch is that you that you press keys but never release them.
Further I suggest that you create a safe-guard. The below is the framework
#include <Keyboard.h>
#define SAFETY_ENABLED HIGH
const uint8_t safetyPin = A0;
void setup()
{
pinMode(safetyPin, INPUT_PULLUP);
}
void loop()
{
if (digitalRead(safetyPin) == !SAFETY_ENABLED)
{
Keyboard.println("Hello World");
delay(1000);
}
}
The above uses pin A0 as a safety pin; to send data to the PC, you need to short A0 to ground. If you have problems where your code spams the PC, you can at least prevent that by removing the wire between A0 and GND. Or in your case, you don't have to go through the whole excercise above (PC might still need a reboot).