One view holds that the construction about which you ask results from a determination to avoid the use of the pronoun "Whom," even when it is the best choice, and even when avoiding it results in a sentence like yours, which ends in a preposition.
Another view holds that use of the pronoun "Whom" (see below) is forced upon us by a determination to abide by the "rule" against ending a sentence with a preposition, even when that is the best choice.
The pronoun "whom" is the form of "who" used as a direct or indirect object, and if you substitute it for "Who" in your sentence, and order the sentence properly, it looks like this:
To whom did you give the keys?
That is to say:
Question word + auxiliary verb + infinitive without to + subject
In everyday speech, though, you are likely to hear the sentence as you originally presented it, and the "rule" which forbids ending a sentence with a preposition (which is actually more appropriate to Latin than English) is routinely violated, and for good reason. A quotation usually (and probably erroneously) attributed to Winston Churchill puts it like this:
This is the sort of English up with which I will not put!