Help with current program

I have been working on a project to help learn programming/microcontrollers and am not sure how to get past this one last hurdle on what I would like to do.

I am taking a SNES controller and having it output to a PC as a joystick. I am using a Leonardo with modified HID and USBAPI files that include joystick support. I am also using a library written to gather to data from the SNES button presses. The way it works is it has a double 8 bit shift register, the first 12 bits are the states of the 12 buttons and the last 4 are always high.

The state of the buttons are stored as an unsigned integer.

What I am trying to add in are some Keyboard presses when certain buttons are pressed like follows.

  if(btns & BTN_SELECT)
  {
      joySt.buttons = joySt.buttons | 1;
      Keyboard.press('n');
      Keyboard.release('n');
    }

The key presses do indeed get sent out, however it sends out 16 of them. I am guessing this has to do with the state of the buttons being store as an integer? What would be the best method to implement to have this only output a single keypress when they actions are done?

Sorry if I did not explain as well, any further questions please just let me know.

have you got the arduino board working as a usb master device? or are you using the pc as an interface repeating the input to the arduino? I ask as im using a usbhost shield to talk to a usb slave device?

have you checked out the usb coms data here https://www.circuitsathome.com/
i think you might find the code you need in the usb host shield libraries?

I guess it is a host?

I am just using the built in USB functions of the Leonardo. It is shown to Windows as just being a Keyboard/mouse and Joystick.

Need to see the code of how your reading the switches..
Quick guess is that you need debounce code added. It's probably reading the button switch 16 times when your tapping it..

Here is the cpp and h file for the library

cpp

#include "Arduino.h"
#include "SNESpaduino.h"

// Constructor: Init pins
SNESpaduino::SNESpaduino(byte latch, byte clock, byte data)
{
	// Store the latch, clock and data pin for later use
	PIN_LATCH = latch;
	PIN_CLOCK = clock;
	PIN_DATA = data;

	// Set correct modes for the communication pins
	pinMode(PIN_LATCH, OUTPUT);
	pinMode(PIN_CLOCK, OUTPUT);
	pinMode(PIN_DATA, INPUT);

}

// Return the state of all buttons. 12 of the uint16_t's bits are used, the 4 MSBs must be ignored.
uint16_t SNESpaduino::getButtons(boolean return_inverted)
{
	// Init the button-state variable
	state = 0;

	// Latch the current buttons' state into the pad's register
	digitalWrite(PIN_LATCH, HIGH);
	digitalWrite(PIN_LATCH, LOW);

	// Loop to receive 12 bits from the pad
	for(i = 0; i < 12; i++)
	{
		// Read a button's state, shift it into the variable
		state |= digitalRead(PIN_DATA) << i;

		// Send a clock pulse to shift out the next bit
		digitalWrite(PIN_CLOCK, HIGH);
		digitalWrite(PIN_CLOCK, LOW);
	}

	// Return the bits
	if(return_inverted)
		return ~state;
	else
		return state;
}

h

#ifndef snesduino_h
#define snesduino_h

#define BTN_B 0x01
#define BTN_Y 0x02
#define BTN_SELECT 0x04
#define BTN_START 0x08
#define BTN_UP 0x10
#define BTN_DOWN 0x20
#define BTN_LEFT 0x40
#define BTN_RIGHT 0x80
#define BTN_A 0x100
#define BTN_X 0x200
#define BTN_L 0x400
#define BTN_R 0x800

#include "Arduino.h"

class SNESpaduino
{
	public:
		SNESpaduino(byte latch, byte clock, byte data);
		uint16_t getButtons(boolean return_inverted = true);
	private:
		uint16_t state;
		byte i, PIN_LATCH, PIN_CLOCK, PIN_DATA;
};

#endif

Thanks to an awesome guy, Logan O, who has fixed the issue. Turns out the release had to be placed in an else statement.