The only thing on the card key is a rotating code and a serial code of some sort to distinguish one key from another (i.e. two master keys will have the same key code but different serial codes.) When you check in, the clerk puts the key in an encoding machine that records onto it the next sequential code for the lock on your room. When you first enter your room the lock recognizes that the next sequential code is being used and instantly invalidates the previous key. That explains why if you get a second key later for someone else, it often messes up your key - the clerk coded a new key rather than a duplicate. The sequence change means that the previous guest can no longer get into the room.