firstly - they are not keys but notes/tones
melodies will relate back to two things with more emphasis given to the one than the other
Chords - the active chord will suggest a temporary home key key/scale/mode according to chord-scale theory (an advanced area of jazz/pop theory)
and
home key - the home key scale/mode will also suggest the more usual path to use notes to create the melody
for example
If I have the chord progression C-F-C-G-C-F-C-D-G (all major chords) the following scales are some possibilities for each chord:
C ~ C major (C-D-E-F-G-A-B-C), G Major (G-A-B-C-D-E-F#-G), F major (F-G-A-Bb-C-D-E-F)
F ~ F major (F-G-A-Bb-C-D-E-F), C major (C-D-E-F-G-A-B-C), Bb Major (B-C-D-Eb-F-G-A-Bb)
G ~ G Major (G-A-B-C-D-E-F#-G), D major (D-E-F#-G-A-B-C#-D), C major (C-D-E-F-G-A-B-C)
D ~ D major (D-E-F#-G-A-B-C#-D), G Major (G-A-B-C-D-E-F#-G), A major (A-B-C#-D-E-F#-G#-A)
however for the most part we are likely to use the scale of C major throughout until the D major chord where we would use the scale of G major temporarily to create a sense of closure to the G chord - the G chord itself would not use the F# but an F to promote the movement back to C major