The choice of the most appropriate preposition depends, often, on the context:
1. He knocked on the window. [the meaning in obvious: the window is where he knocked]
2. He knocked at the window. [there is a slight connotation that he knocked at the window instead of a more obvious place, like the door]