Under what conditions were the words to the star spangled banner written?

In the War of 1812, Francis Scott Key had been escorted aboard a British ship and detained there during the Battle of Fort McHenry in Baltimore harbor. The British pounded the American fort relentlessly with canon fire that night but when the morning came Key saw the huge flag of the fort still there and that the Americans had not given up. This inspired him to write the star spangled banner.