Handling keycodes in loop