In a city of millions of keys, this is a story of two of them. Two keys carried by two strangers that opened the same apartment door. Twice, the police said, one man let himself inside while the other was away. The first time, the victim was just that — a victim. The second time, he was ready.

The tenant, a college professor of technology, had moved into the apartment in April. It was a quiet walk-up on East 11th Street in Manhattan. The superintendent told the professor, Mr. Lee, that the lock had just been changed.

Two months later, on June 12, someone entered the apartment while Mr. Lee, who did not want his full name disclosed, was at work. The burglar stole two video game consoles and other electronics. There was no sign of forced entry, nor any witnesses.

“I decided to be more defensive,” he said.

The technology professor tried a couple of surveillance cameras before settling on one made by Dropcam. He placed it in a room where he kept electronic valuables, and set it to constantly record, with the last seven days of video stored online.