To add on to what jt3n1 was saying, once you get in a certain key, translate all the letters on the keyboard to fit that key. If you playing in a key with sharps in the key signature, then your keyboard looks like this, for the most part:
C/B#, C#, D, D#, E, E#/F, F#, G, G#, A, A#, B
If you playing in a key with flats in the key signature, then your keyboard looks like this, for the most part:
C, Db, D, Eb, E/Fb, F, Gb, G, Ab, A, Bb, B/Cb
You get the idea? If you translate all your notes into the key you playing in, then theoretically every note and chord you hit is in that key.