Attack animation problem

I don’t want my character to hit in the air, he has the “M” key assigned to hit and it works fine, the problem is that I don’t want the “M” key to work in the air. How can I fix the problem?