Problem with keyboard emulation on Leonardo

I've been working on coding a custom controller using keyboard emulation and have hit a bit of a brick wall. I'm not overly familiar with programming with C but according to all my reference material this should work. However once I load it onto the board it goes nuts. Could someone point out where I've gone wrong because I just don't see it.

int button1 = 2;
int button2 = 3;
int button3 = 4;
int button4 = 5;
int button5 = 6;
int button6 = 7;
int button7 = 8;
int button8 = 9;
int button9 = 10;
int button10 = 11;
int button11 = 12;
int button12 = 13;

void setup() {
  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT_PULLUP);
  pinMode(button3, INPUT_PULLUP);
  pinMode(button4, INPUT_PULLUP);
  pinMode(button5, INPUT_PULLUP);
  pinMode(button6, INPUT_PULLUP);
  pinMode(button7, INPUT_PULLUP);
  pinMode(button8, INPUT_PULLUP);
  pinMode(button9, INPUT_PULLUP);
  pinMode(button10, INPUT_PULLUP);
  pinMode(button11, INPUT_PULLUP);
  pinMode(button12, INPUT_PULLUP);
  Keyboard.begin();
}

void loop() {
  //Power Management
  if (!digitalRead(button1))
    Keyboard.press(KEY_UP_ARROW);
  Keyboard.release(KEY_UP_ARROW);

  if (!digitalRead(button2))
    Keyboard.press(KEY_DOWN_ARROW);
  Keyboard.release(KEY_DOWN_ARROW);

  if (!digitalRead(button3))
    Keyboard.press(KEY_LEFT_ARROW);
  Keyboard.release(KEY_LEFT_ARROW);

  if (!digitalRead(button4))
    Keyboard.press(KEY_RIGHT_ARROW);
  Keyboard.release(KEY_RIGHT_ARROW);

  //Engine boost
  if (!digitalRead(button5))
    Keyboard.press(KEY_TAB);
  Keyboard.release(KEY_TAB);

  //Cargo scoop
  if (!digitalRead(button6))
    Keyboard.press(KEY_HOME);
  Keyboard.release(KEY_HOME);

  //Ship lights
  if (!digitalRead(button7));
  Keyboard.press('l');
  Keyboard.release('l');

  //Landing gear
  if (!digitalRead(button8))
    Keyboard.press(KEY_INSERT);
  Keyboard.release(KEY_INSERT);

  //Frameshift
  if (!digitalRead(button9))
    Keyboard.press('j');
  Keyboard.release('j');

  //Silent running
  if (!digitalRead(button10))
    Keyboard.press(KEY_DELETE);
  Keyboard.release(KEY_DELETE);

  //Target highest threat
  if (!digitalRead(button11))
    Keyboard.press('h');
  Keyboard.release('h');

  //Target ahead
  if (!digitalRead(button12))
    Keyboard.press('t');
  Keyboard.release('t');

  delay(150);
}

Could someone point out where I've gone wrong because I just don't see it.

Sure. Right here:

However once I load it onto the board it goes nuts.

That's hardly a technical description of what the code actually does.

Why are you calling Keyboard.release() for keys you haven't pressed?

Sorry, I'll try to be more specific. The moment I load the code onto the board it will just begin activating all commands at once without any push button trigger.

Edit: Also, like I said, my programming knowledge isn't great so I expect I may have added superfluous lines of code.

The moment I load the code onto the board it will just begin activating all commands at once without any push button trigger.

What is connected to the pins?

I really don't like this:

  if (!digitalRead(button1))

digitalRead() does not return true or false.

   if(digitalRead(button1) == LOW)
   {
      // Switch is pressed
      Keyboard.press(KEY_UP_ARROW);
      Keyboard.release(KEY_UP_ARROW);
   }

is not that much more typing, yet it is VERY clear what result you expect from digitalRead(), and what you expect to have happen when that result is obtained.

The circuit for each pin is currently PIN>Jumper to breadboard>Push button/momentary switch>Ground. Depending on which button is pressed the result should be the associated emulated keyboard button press. For example the button on pin 2 should result in the letter J being pushed.

For example the button on pin 2 should result in the letter J being pushed.

The value, 2, is assigned to button1.

  if (!digitalRead(button1))
    Keyboard.press(KEY_UP_ARROW);
  Keyboard.release(KEY_UP_ARROW);

That doesn't look like a J to me...

My bad. Currently rewriting the code. Obviously by that code the button on pin 2 should trigger the up arrow to be pressed.

Ok, so I finished rewriting it and cleaned it up a little and now it works perfectly. However I still fail to see the difference between this version and the version I posted at the top of the thread. Can anyone tell me what I'm missing? Was it the repeated Keyboard.release strings?

int button1 = 2;
int button2 = 3;
int button3 = 4;
int button4 = 5;
int button5 = 6;
int button6 = 7;
int button7 = 8;
int button8 = 9;
int button9 = 10;
int button10 = 11;
int button11 = 12;
int button12 = 13;

void setup() {

 pinMode(button1, INPUT_PULLUP);
 pinMode(button2, INPUT_PULLUP);
 pinMode(button3, INPUT_PULLUP);
 pinMode(button4, INPUT_PULLUP);
 pinMode(button5, INPUT_PULLUP);
 pinMode(button6, INPUT_PULLUP);
 pinMode(button7, INPUT_PULLUP);
 pinMode(button8, INPUT_PULLUP);
 pinMode(button9, INPUT_PULLUP);
 pinMode(button10, INPUT_PULLUP);
 pinMode(button11, INPUT_PULLUP);
 pinMode(button12, INPUT_PULLUP);
 Keyboard.begin();
}

void loop() {

//Frameshift
 if (!digitalRead(button1))
       Keyboard.press('j');
       
//Landing gear
 if (!digitalRead(button2))
       Keyboard.press(KEY_INSERT);
       
//Cargo scoop
if (!digitalRead(button3))
       Keyboard.press(KEY_HOME);
       
//Hardpoints
if (!digitalRead(button4))
       Keyboard.press('u');
             
//Power management
//Balance
if (!digitalRead(button5))
      Keyboard.press(KEY_DOWN_ARROW);

//Engines
if (!digitalRead(button6))
      Keyboard.press(KEY_UP_ARROW);

//Systems
if (!digitalRead(button7))
      Keyboard.press(KEY_LEFT_ARROW);
     
//Weapons
if (!digitalRead(button8))
      Keyboard.press(KEY_RIGHT_ARROW);
      
//Engine boost
if (!digitalRead(button9))
      Keyboard.press(KEY_TAB);

//Ship lights
if (!digitalRead(button10))
      Keyboard.press('l');

//Disable flight assist
if (!digitalRead(button11))
      Keyboard.press('z');

//Highest threat
if (!digitalRead(button12))
      Keyboard.press('h');

Keyboard.releaseAll();
delay(150); 
}

This code calls Keyboard.ReleaseAll() which only releases pressed keys. The other code releases keys that may not be pressed. Who knows what grief that causes. Well, actually, you do.

Yes, I do. Thanks a lot for helping me work through this, PaulS.