A man peeked through a hole into the Prophet's ﷺ room while the Prophet ﷺ had a comb with which he was scratching his head. He said, "If I had known you were looking, I would have struck your eye with it. Permission to enter was prescribed only because of the gaze."
اطلع رجل من جحر في حجر النبي ﷺ، ومع النبي ﷺ مدرى يحك به رأسه، فقال: لو أعلم أنك تنظر، لطعنت به في عينك، إنما جعل الاستئذان من أجل البصر.
Bukhari reported it in the Book of Seeking Permission (6241), and Muslim in the Book of Manners (2156), both through chains from Zuhri, from Sahl bin Saad, who mentioned it. The wording is Bukhari's.