KEYS: I ii iii IV V vi vii   viii
      C Dm Em  F  G Am Bdim  C
      D Em F#m G  A Bm C#dim D
Note widths are Whole Whole Half Whole Whole Whole Half 
for these major scales. A half note separates EF and BC. 
The notes:     12345678
               CDEFGABC
The chords: C(CEG), Dm(DFA)....
Cma7 (CEGB)
If a song used E5 A5 and B5 the key is E (I IV V).
If the Whole(W) and Half(H or 1/2) pattern is not WWHWWWH
the the chord is a minor (a half note in wrong place) or
diminished (two half notes in the pattern).

C   (CEG): C W W E H W G W W B H W D
Dm  (DFA): D W H F W W A W H C W W E
Em  (EGB): E H W G W W B H W D W H F
F   (FAC): F W W A W H C W W E H W G
G   (GBD): G W W B H W D W H F W W A
Am  (ACE): A W H C W W E H W G W W B
Bdim(BDF): B H W D W H F W W A W H C

Chart expanded to 7th and 9th (12/05/2005).
All guitar frets are 1/2 note separations, piano black
keys are 1/2 above note at left ( called a sharp (#) )
or 1/2 below note at right ( called flat (b) ).

Chord notes are counted as root(1), 3rd and 5th thus CEG and the rest. 
Which came first I don't know but Pythagoras put down many keys after
finding tube chimes or strings sound the same if cut in half but give
a different sound with a 2/3 length. The rest is history.

1