Improved if condition for the space key event