The checking for the state (0 or 1) should be nested under the On key pressed event, not the other way around.
Also, I'd suggest you make the state variable a boolean instead of a number. That would reduce the number of events since there's an action Toggle boolean.
In the example below I did both things, but I changed the state variable to a On variable which is a boolean and may be True or False.
Hope this helps... Cheers!