This is 3 questions in one, so it might take a slightly involved answer, or some good links.
Edit--- tl;dr:
- How can you learn which keys to hit to make the notes you hear in your head? I know what I want to play when I'm improvising, I can hear it in my head, but I don't know which keys correspond to those notes.
- How can you learn which chords will sound good where? There are so many types of chords, how can you know when to play a certain one?
- How can you learn to know when accidentals (notes outside the scale) will sound good?
I play piano fairly well, but I skipped theory entirely in my early days and learned to play by reading sheets and listening to better pianists online playing the same piece I was learning, then carefully emulating their sound.
But now I've made some friends who like to improvise together on the piano and do it extremely well, and I'm at a loss when I join in. They usually take the lower register and establish a beautiful rhythm, while I take the upper register and toy around with a melody.
I have no trouble keeping time with them, and I sometimes come up with very pretty melodies, but then here's what happens: I hit 2-3 notes and in my head I hear a melody continuing with the pattern of those 2-3 notes, but then I don't know what notes I'm hearing in my head (I don't have any sort of pitch, not even relative, I'm pretty much tone-deaf), so I can't continue the melody in real time because I don't know what keys to hit in order to create the notes I'm hearing in my head. How do you learn to know what notes to hit? Right now I just have to guess, and sometimes I get it, and sometimes I don't.
Problem two: while toying around with the melody, I'm pretty much trapped in single notes and octaves. Obviously in all the pieces I've played written by other people, there are a million different chords of 2, 3, 4, 5, even 6 notes, and they sound beautiful. But whenever I try to hit one, it just doesn't sound right. How can I learn which ones achieve what kind of sound and when they'll belong and when they won't?
Lastly: The way we improvise is my friend picks a key and shows me what the key is, then I'm careful to only hit notes in that key. For instance, if she said F major, she would show me that B is flat (because I don't know my scales by name), and then while improvising I would be careful not to hit any other black keys and not to hit B natural, which are not in the scale. But I know from all the other pieces I've played that you don't have to stay inside the scale in order for it to sound right-- obviously pieces have sharps and flats and naturals outside the scale all the time. But whenever I try to do it while improvising, it usually just sounds dead WRONG, instantly. But every now and again, by chance, it sounds so right. How do you know when it's okay to go outside the scale?
Sorry for long question!