In order to build the key signature of a major scale or tonality, it is important to remember that the major scale of C does not have any key signature. On the contrary, any other scale may have sharps or flats.
All major scales based on a flat note, G flat, E flat, D flat, etc. use key signatures with flats. The only exception is F major. Therefore, when you are looking for a key signature, you can assume that it uses sharps if it is not F or does not begin with a flat note:
Once the accidentals used in the key signature are known, the following procedure may be used to build the key signature:
Follow the order of the sharps until you reach the one that is a half-step below the major scale chosen. For instance, A major, F#, C#, G#. G# is a half-step below A. Therefore, sharps are F#, C#, G#.
Follow the order of the flats until you reach the one following the one that bears the same name in the major scale chosen. For instance, A flat major, B flat, E flat, A flat, D flat. D flat comes after A flat; therefore, flats are B flat, E flat, A flat, and D flat.
This method does not apply to the major scale of F that has a flat. Therefore, this key signature has to be memorized.
See S > Scales for related topics.