It's part of collision detection. Took a lot of trial and error to make it work. Used a lot of global variables. For the key, I created a global variable called window.GOLDKEYATTACHED and set it initially equal to "no". Depending on which side the player bumps into it, it takes on the value of "top", "right", "bottom" or "left". I use this to determine the key position based off of the player position. When you press spacebar, the value goes to "no" and you drop the key.
For collision detection in general, I created a 2D array which is a grid of the scene. If a grid contains the value 0, you can move into it. I used 1 to represent a wall, 2 for the gold key, 3 for the black key, etc.
Thanks for playing and testing!