Refactors and break the loop if a key is pressed:
That makes the pulse non blocking