A key signature is a group of sharps or flats which are printed at the beginning of a line/measure of music. It shows which notes have to be changed into sharps or flats. For example: if there is one sharp in the key signature it will be an F sharp. It means that every time the note F is written one plays (or sings) an F sharp (on a keyboard: the black note just to the right of the F) instead.
There are twelve major and twelve minor keys (properly called “modes"). This is because a scale can start on any note, and there are twelve notes within an octave: seven white notes and five black notes on a keyboard. Each major key has its own key signature. The relationship between the keys is explained in the article "circle of fifths". Each minor key shares a key signature with one of the major keys.
There are fifteen possible key signatures: up to seven sharps, up to seven flats, or no sharps or flats. The reason why there are fifteen and not twelve is because three of them have two possible names: F sharp major (6 sharps) can also be called G flat, C sharp is D flat and B is C flat. We call these enharmonic i.e. the same note but with a different name.
There are two reasons for writing a key signature. Firstly, it saves writing out lots of sharps or flats during the piece. Secondly, it helps the player to think in the key (music) of the piece. This helps to understand the music better.
If the composer wants any extra sharps or flats during the piece, or he wants to cancel the ones in the key signature, these can be written in the music. These are called accidentals. An accidental is always written before the note it belongs to (we say “C sharp” but we write the sharp sign and then the note C). As an example: in a piece in G major all Fs are F sharps. If the composer wants a C sharp he writes a sharp sign in front of the C that needs to be sharpened. This will last for the rest of the bar (measure). If he wants a B flat he has to write it in. If he wants an F instead of the usual F sharp he writes a natural sign. There are also double sharps (the sign looks like an x) and double flats (two flat signs). An F double sharp is an F sharp that has been sharpened. It is the same note as G.
Key signatures are written at the beginning of each line of music. This helps the player to remember what the key signature is. The time signature, however, is only written at the beginning of the piece (the order is: clef – key signature – time signature). Sometimes music changes key (modulates) during a piece. If the music is going to stay in the new key for some time the composer may decide to change the key signature. Another reason for changing the key signature might be that the music has modulated to a key like G sharp major with eight sharps (including a double sharp). It is easier to read the music if it is written in A flat major (4 flats).
One can tell from the key signature what key a piece is in so long as one knows whether it is major or minor (see “mode”). For example: a key signature of one sharp is either G major or E minor. The best way to find out which it is would be to see whether the end of the piece is based on G major or E minor. Also: if the piece is in the minor key it is very likely that there will be several accidentals in the piece which sharpen the 7th note of the scale (in this case changing a lot of the Ds to D sharps).
Some modern composers do not use key signatures. This is often the case when the music is atonal or not very firmly in one key. If there are lots of sharps and flats and the bars are very long they may write accidentals in front of every note that needs one instead of just once in a bar. This should be explained at the top of the music. It may also be because the piece is modal.
One way to read key signatures is to use the order of sharps and flats. The order of sharps is F-C-G-D-A-E-B. This means that if a piece has two sharps at the beginning, they will be F and C. You can also find the tonic note (“do” in solfege) for key signatures with sharps by going to the note you left on (so, in our example with the two sharps, would be C) and moving up a note in the alphabet, giving us D as our home note. It’s a bit different for the order of flats, which is B-E-A-D-G-C-F. We will still count the flats, meaning that if there are two flats at the beginning they will be B and E, but we find the tonic note differently. For the order of flats, we will go back one in the order, meaning that in our example our home note is B flat. If there is only one flat in the key signature, the tonic is F since it’s at the end of the order. If there are no sharps or flats in the key signature, the tonic is C.
No sharps or flats
|B flat major
|E flat major
|A flat major
|D flat major
|B flat minor
|G flat major
|E flat minor
|C flat major
|A flat minor