The order of which guard has which key is determined by the character naming screen whenever you start a new prison. This is done by pressing a hotkey when the key is hovered over. On certain versions (Console TE2, Mobile TE1) having a Wad of Putty in your inventory allows you to make a Key Mold without taking the original key.
Then make Molten Plastic, craft it together with the Key Mould to make a Plastic Key. Then open the guard's inventory up again and replace the original key. Craft the key together with the wad of putty to make a key mould out of the color key.
Then knock out the desired guard, open his inventory, and remove the key.
In order to make a Key Mould, first make a Wad of Putty. Key Moulds can only be made by crafting and are never found in inmates' desks or sold by inmates. Key Moulds are crafted items which allow you to make a plastic copy of normal colored Keys carried by specific Guards. You want a key? You'll have to mould it first!