NEWTON, Kan. — Three people were killed at a manufacturing plant in Hesston, Kan., on Thursday by a man who had driven through at least two towns shooting a gun out of his car window, the police said.

The gunman was killed in a gunfight with a police officer inside the factory, the authorities said.

T. Walton, the sheriff of Harvey County, told reporters that the shootings occurred around 5 p.m. at the factory and on the roads between Hesston and Newton, Kan., where the gunman lived. The factory is owned by Excel Industries, a company that makes riding mowers and other lawn care equipment.

Sheriff Walton said that four people, including the gunman, had been killed at the factory and that 14 others were injured, 10 of them critically. The gunman, who worked at the plant, shot three more people as he drove to the factory, including a man whose car he stole on the way there. He shot a woman in the parking lot at the factory before entering the building and opening fire.