As I understand it the fifth is generated by always adding seven to the MIDI note number, and the third is generated by adding four.
Correct - even more specifically, the perfect fifth is generated by always adding seven to the MIDI note number, and the major third is generated by adding four.
However, I don't think it works like that because adding a fixed number will result in a major or minor triad depending on the starting note.
Don't worry, it does work like that - starting with a root note and adding intervals with a fixed number of semitones, which is what the MIDI note numbers represent, will always result in the same chord.
When you talk about "major or minor depending on the starting note", maybe you are thinking of intervals between degrees of the diatonic scale, like 'a third', which can have different sizes. For example, on the major scale, the third between degrees 1 and 3 is a major third, and the third between 2 and 4 is a minor third. Another way of looking at this is that the diatonic scale 'skips' some notes in the chromatic scale, which is why you get different numbers of semitones, and why triads based on different degrees of a major scale are sometimes major, sometimes minor.
But MIDI note numbers aren't based on degrees of the diatonic scale - they're purely numbers of semitones. The formula for each type of chord in terms of number of semitones is always the same.
Is there a way to generate all major ( or minor ) triads for any given root note?
You already have the formula for generating major triads. For minor triads, just add 3 to get the minor third, and 7 to get the perfect fifth.
I'm not sure what your use case for generating all major or minor chords, but note that a minor key does not always use minor chords, and a major key does not always use major chords!
To add some further information. My controller outputs in the key of C major. In order to transpose this to any key I am adding a constant number to the MIDI notes. In addition when I am in this key I want a switch to give the option of generating a minor triad or a major triad along with the note played. Therefore I believe I must consider the distance of the note being played relative to the root note of the key I am in.
So everything we said so far about how to generate major and minor chords is correct - but if I understand correctly, you also want to generate the 'right' chord based on the degree of the scale. To do this, we have to make some assumptions as to what the 'right' chords are, but a common starting point in Western music is to assume the diatonic chords of the scale.
You say that you know your controller always outputs in the key of C major, so you might actually save yourself some logic if you work out the chords at that point, because you know the 'root note' of the scale is C - midi note 60, which is, handily, also a multiple of the number of semitones in the chromatic octave.
Let's assume that we are using the major scale, so that we want to generate the diatonic chords of the major scale.
if (note number % 12) = 0, you play a major chord.
if (note number % 12) = 2, you play a minor chord.
if (note number % 12) = 4, you play a minor chord.
if (note number % 12) = 5, you play a major chord.
if (note number % 12) = 7, you play a major chord.
if (note number % 12) = 9, you play a minor chord.
if (note number % 12) = 11, you play a diminished chord (root, root+3, root+6)
From that point, you can do your overall transposition to shift the output to the right key.
Of course that still leaves some questions unanswered. Will you have a separate way that the player can indicate minor keys, or will they just have to set the transposition suitably and play starting from A? What if a player plays a note that's not in the diatonic scale, or wants a chord that's not in the diatonic set of chords? There aren't any 'correct' answers to those questions - you'll have to find the right balance between flexibility and ease of use.