"He has a key."
Translation:Er hat einen Schlüssel.
Ein changes its ending according to the gender and case of the noun it describes. Here, Schlüssel is masculine (it's der Schlüssel) and accusative (it's the direct object of hat), so it's einen Schlüssel.
If it had been, for example, "A key is black", it would still be masculine (gender never changes) but would now be nominative (it is the subject of the sentence), so it would ein Schlüssel).
If it had been "She hits him with a key", that would be dative (with = mit which always takes the dative) which is einem Schlüssel.
The full table of ending changes for ein is here in the Wikipedia article: http://en.wikipedia.org/wiki/German_articles#The_Indefinite_Article
In Marfh 2017, my immersion German instructor in Munich told all 10 of us this the first day: "For every noun, learn its gender and its plural form." She was so right. There is no dependable pattern, only guidelines with tons of exceptions (even a funny little song). Three years later and I still struggle!
Ein is used in nominative for neutral and masculine words (das/der); feminine words (die) use eine. In the accusative case, you get ein/einen/eine for das/der/die, respectively. "Der" is also replaced by "den" in the accusative case, e.g. "Ich sehe den Park".
See the notes for this lesson for a better and more thorough explanation: https://www.duolingo.com/skill/de/Accusative-Case/tips-and-notes
gorn61 is right.
The principle has to do with what is happening in the sentence and to whom or what (and the gender of that 'whom or what'. (It has nothing to do with the verb tense.)
I had to go back to some English grammar principles because I was soooo confused. I am still confused, but at least I have an understanding of why things change. I used these sites to help me.