UNO joystick, matrix inputs

i am absolutely useless at programming, i can't even figure out what should be something simple:
But i am determined to learn at least part of what i am doing with the arduino's.

I want to turn my uno (R3) into a usb hid joystick, i have done so using Darran Hunts 'UNO big joystick' http://hunt.net.nz/users/darran/

I like this particular version of the HID usb joysticks as it has the number of buttons i need, and it 'looks' simple,

So i have the following script on my UNO:

   /* Arduino USB Joystick HID demo */
    /* Author: Darran Hunt
     * Released into the public domain.
     */

    #define NUM_BUTTONS	40
    #define NUM_AXES	8	       // 8 axes, X, Y, Z, etc
    typedef struct joyReport_t {
	int16_t axis[NUM_AXES];
	uint8_t button[(NUM_BUTTONS+7)/8]; // 8 buttons per byte
    } joyReport_t;

    joyReport_t joyReport;

    void setup() 
    {
	Serial.begin(115200);
	delay(200);

   	for (uint8_t ind=0; ind<8; ind++) {
	    joyReport.axis[ind] = ind*1000;
	}

	for (uint8_t ind=0; ind<sizeof(joyReport.button); ind++) {
	    joyReport.button[ind] = 0;
	}
    }

    // Send an HID report to the USB interface
    void sendJoyReport(struct joyReport_t *report)
    {
	Serial.write((uint8_t *)report, sizeof(joyReport_t));
    }

    // turn a button on
    void setButton(joyReport_t *joy, uint8_t button)
    {
	uint8_t index = button/8;
	uint8_t bit = button - 8*index;

   	joy->button[index] |= 1 << bit;
    }

    // turn a button off
    void clearButton(joyReport_t *joy, uint8_t button)
    {
	uint8_t index = button/8;
	uint8_t bit = button - 8*index;

	joy->button[index] &= ~(1 << bit);
    }

    uint8_t button=0;	// current button
    bool press = true;	// turn buttons on?
    /* Turn each button on in sequence 1 - 40, then off 1 - 40
     * add values to each axis each loop
     */
    void loop() 
    {
	// Turn on a different button each time
	if (press) {
	    setButton(&joyReport, button);
	} else {
	    clearButton(&joyReport, button);
	}

	/* Move all of the axes */
	for (uint8_t ind=0; ind<8; ind++) {
	    joyReport.axis[ind] += 10 * (ind+1);
	}
	sendJoyReport(&joyReport);
	button++;
	if (button >= 40) {
	   button = 0;
	   press = !press;
	}
	delay(100);
    }

And when i use 'flip' to load the 'big joystick.hex' to the 8u2, it works, upon unplugging and re-pluggig the UNP, it shows up as a 40 button, 8 axis joystick,
Of course it is only a demo script, so the buttons are turning on one at a time, then off one at a time, and the axis moving by them selves,

I need to replace the demo code with actual code that will read inputs from the digital pins of the UNO, and send out the relevant joystick button presses.
i have no use for the axis, but i will leave it until much later before i see about removing those and changing the USB descriptors.

Can someone tell me what i need to change to get a single button press recognised, i.e. pin 2 connected to ground shows up as button 1 pressed on the joystick, and released when the connection is removed,
then i can hopefully work out how to get a few buttons working and move on from there,

Eventually i want to combine a 36 button keypad matrix script with the joystick script, so the buttons pressed on the keypad send joystick buttons to the game,

The button matrix script is:

#include <Keypad.h>
#include <LiquidCrystal.h>

const byte ROWS = 4; //four rows
const byte COLS = 9; //nine columns
char keys[ROWS][COLS] = {
  {'0','1','2','3','4','5','6','7','8'},
  {'9','A','B','C','D','E','F','G','H'},
  {'I','J','K','L','M','N','O','P','Q'},
  {'R','S','T','U','V','W','X','Y','Z'}
};
byte rowPins[ROWS] = {13, 12, 11, 10}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 8, 7, 6, 5, 4, 3, 2, 1}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
LiquidCrystal lcd(A5, A4, A3, A2, A1, A0);
  
void setup()
{  
  
  lcd.begin(20, 1); // Specify how many columns and rows in the LCD unit
  lcd.clear();
}

void loop()
{
  char key = keypad.getKey();
  
  switch (key)
  {
     case '0':
       lcd.clear();
                lcd.print ("xxx");
     break;
     
     case '1':
       lcd.clear();
                lcd.print ("xxx");
     break;
     
     case 'A':
       lcd.clear();
                lcd.print ("xxx");
     break;
     
     case 'B':
       lcd.clear();
                lcd.print ("xxx");
     break;
     
     case 'C':
       lcd.clear();
                lcd.print ("xxx");
     break;  //and so on for 36 buttons
  }  
}

It is one i used to display different sentences on an LCD according to which button was pressed, so i guess that instead of the LCD bit, i need to 'print' the button press letter to the joystick??

Well, i've been playing but not getting very far,

i've added the code for a basic 4 x 3 keypad to the joystick sketch, but i am stuck on how to make a keypad button press send a USB joystick button press,

my script so far...

/* Arduino USB 12 button matrix joypad */

/* USB HID stuff; Author: Darran Hunt
Matrix stuff by me. based on stock matrix kaypad lib.
 * Released into the public domain.
 */
#include <Keypad.h> //keypad library

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
//define charecters for the buttons of the keypad
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},    //electrical keypad layout
  {'7','8','9'},
  {'A','0','B'}
}; 
byte rowPins[ROWS] = {5, 4, 3, 2,}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS); 

#undef DEBUG

#define NUM_BUTTONS	40            //40 lovely buttons
#define NUM_AXES	8	       // 8 axes, not needed, will delete later

typedef struct joyReport_t {
    int16_t axis[NUM_AXES];
    uint8_t button[(NUM_BUTTONS+7)/8]; // 8 buttons per byte
} joyReport_t;

joyReport_t joyReport;


void setup(void);  
void loop(void);
void setButton(joyReport_t *joy, uint8_t button);
void clearButton(joyReport_t *joy, uint8_t button);  //constantly sending usb data i guess
void sendJoyReport(joyReport_t *report);


void setup() 
{
    Serial.begin(115200);
    delay(200);

    for (uint8_t ind=0; ind<8; ind++) {
	joyReport.axis[ind] = ind*1000;
    }
    for (uint8_t ind=0; ind<sizeof(joyReport.button); ind++) {
        joyReport.button[ind] = 0;
    }
}

// Send an HID report to the USB interface
void sendJoyReport(struct joyReport_t *report)
{
#ifndef DEBUG
    Serial.write((uint8_t *)report, sizeof(joyReport_t));
#else
    // dump human readable output for debugging
    for (uint8_t ind=0; ind<NUM_AXES; ind++) {
	Serial.print("axis[");
	Serial.print(ind);
	Serial.print("]= ");
	Serial.print(report->axis[ind]);
	Serial.print(" ");
    }
    Serial.println();
    for (uint8_t ind=0; ind<NUM_BUTTONS/8; ind++) {
	Serial.print("button[");
	Serial.print(ind);
	Serial.print("]= ");
	Serial.print(report->button[ind], HEX);
	Serial.print(" ");
    }
    Serial.println();
#endif
}

// turn a button on
void setButton(joyReport_t *joy, uint8_t button)
{
    uint8_t index = button/8;
    uint8_t bit = button - 8*index;

    joy->button[index] |= 1 << bit;
}

// turn a button off
void clearButton(joyReport_t *joy, uint8_t button)
{
    uint8_t index = button/8;
    uint8_t bit = button - 8*index;

    joy->button[index] &= ~(1 << bit);
}

uint8_t button=0;	// current button
bool press = true;	// turn buttons on?

/* Turn each button on in sequence 1 - 40, then off 1 - 40
 * add values to each axis each loop
 */
void loop() 
  { char key = keypad.getKey(); //gets keypad inputs
  
  /*Now what? 
  how do i tell it to send joystick button 1 as pressed when keybad number 1 is pressed, 
  jotstick button 2 as pressed on keypad button 2 and so on??
  
  
  The code below is what made the buttons turn on in sequence, then back off as well as move the axis.
  
  
    // Turn on a different button each time
    if (press) {
	setButton(&joyReport, button);
    } else {
	clearButton(&joyReport, button);
    }

    // Move all of the axes
     for (uint8_t ind=0; ind<8; ind++) {
	joyReport.axis[ind] += 10 * (ind+1);
    }

    sendJoyReport(&joyReport);

    button++;
    if (button >= 40) {
       button = 0;
       press = !press;
    }
    delay(100); */
}

Please give me some pointers.

#include <Keypad.h>
#include <Joystick.h>
 
Joystick_ MFD(
    JOYSTICK_DEFAULT_REPORT_ID, 
    JOYSTICK_TYPE_JOYSTICK,
    40, //botones
    0,  //no hat switches
    false, false, false, false, false, false,
    false, false, false, false, false);
  
  const bool initAutoSendState = true;
  int button_0_state = 0;
  int button_value = 0;
  
 
const byte FIL = 5; //cinco filas
const byte COL = 8; //ocho columnas

char keys[FIL][COL] = {
  { 1, 2, 3, 4, 5, 6, 7, 8},
  { 9,10,11,12,13,14,15,16},
  {17,18,19,20,21,22,23,24},
  {25,26,27,28,29,30,31,32},
  {33,34,35,36,37,38,39,40}
};



byte FIL_Pin[FIL] = {5, 4, 3, 2, 1}; //pines a conectar en para las filas
byte COL_Pin[COL] = {13, 12, 11, 10, 9, 8, 7, 6}; //pines a conectar para las columnas
 
Keypad keypad = Keypad( makeKeymap(keys), FIL_Pin, COL_Pin, FIL, COL );
 
void setup(){
  //Serial.begin(9600);
  MFD.begin();
  keypad.addEventListener(keypadEvent); // Add an event listener for this keypad  
}

void loop() {
  keypad.getKey();
}


void keypadEvent(KeypadEvent key){
    switch (keypad.getState()){
    case PRESSED:
        MFD.setButton(key-1, 1);
//        Serial.print("press: ");
//        Serial.println(key, DEC);
        break;

    case RELEASED:
        MFD.setButton(key-1, 0);
  //      Serial.print("release: ");
    //    Serial.println(key, DEC);
        break;
    case HOLD:
        break;
    }
}

ofCross:

#include <Keypad.h>

#include <Joystick.h>

Joystick_ MFD(
    JOYSTICK_DEFAULT_REPORT_ID,
    JOYSTICK_TYPE_JOYSTICK,
    40, //botones
    0,  //no hat switches
    false, false, false, false, false, false,
    false, false, false, false, false);
 
  const bool initAutoSendState = true;
  int button_0_state = 0;
  int button_value = 0;

const byte FIL = 5; //cinco filas
const byte COL = 8; //ocho columnas

char keys[FIL][COL] = {
  { 1, 2, 3, 4, 5, 6, 7, 8},
  { 9,10,11,12,13,14,15,16},
  {17,18,19,20,21,22,23,24},
  {25,26,27,28,29,30,31,32},
  {33,34,35,36,37,38,39,40}
};

byte FIL_Pin[FIL] = {5, 4, 3, 2, 1}; //pines a conectar en para las filas
byte COL_Pin[COL] = {13, 12, 11, 10, 9, 8, 7, 6}; //pines a conectar para las columnas

Keypad keypad = Keypad( makeKeymap(keys), FIL_Pin, COL_Pin, FIL, COL );

void setup(){
  //Serial.begin(9600);
  MFD.begin();
  keypad.addEventListener(keypadEvent); // Add an event listener for this keypad 
}

void loop() {
  keypad.getKey();
}

void keypadEvent(KeypadEvent key){
    switch (keypad.getState()){
    case PRESSED:
        MFD.setButton(key-1, 1);
//        Serial.print("press: ");
//        Serial.println(key, DEC);
        break;

case RELEASED:
        MFD.setButton(key-1, 0);
  //      Serial.print("release: ");
    //    Serial.println(key, DEC);
        break;
    case HOLD:
        break;
    }
}

como veo que hablas español te lo pregunto así, he modificado tu código para que sea de 16 botones únicamente, pero como lo quiero para hacerme un controlador para star citizen...

Como añadiría a ese código las 6 entradas analógicas que me permite arduino uno?

seria para meterle Throttle, power, scanner, system, shields y cooler (en realidad estoy copiándome un blackhog y les he puesto los nombres de la plantilla de Space Sim mod. A1)

Muchisimas gracias