If it's not a joke/excuse as another comment mentioned, it might be connected to how some people sneeze when they are exposed to sunlight. Maybe it started like that for him, and then became associated with taking the hat off enough to trigger it inside as well.