1. He said to me,"Where do you live?"

2. He asked me where I lived.

3. He said, "Where is the key?"
4. He asked where the key was.
(Is #4 grammatical?)

Thank you for your help.

1. He said to me,"Where do you live?"

(is this expressions wrong?)

1. He asked me,"Where do you live?"

2. He asked me where I lived.

3. He asked, "Where is the key?"
4. He asked where the key was.

[Yes, #4 is fine.]

Yes, sentence #4 is grammatically correct. In reported speech, when we report someone's question, we generally change the tense of the verb in the reported clause.

In sentence #3, the direct speech is "Where is the key?" In order to convert it into reported speech in sentence #4, we change the verb tense from "is" to "was" because the reported speech is referring to past events or the past tense.

To answer both of your questions, we can use the reported speech patterns as follows:
1. He said to me, "Where do you live?"
-> He asked me where I lived. (In this case, we change the present tense "do you live?" to the past tense "lived" in the reported speech.)

3. He said, "Where is the key?"
-> He asked where the key was. (Again, we change the present tense "is" to the past tense "was" in the reported speech.)

Note that in reported speech, we use reporting verbs like "asked," "said," "inquired," "wondered," etc., to convey the speech or question of someone else.