So this code is also dangerous. A key event can get canceled and thus the state will be off. We have seen this before when keycode == 13 gets canceled.