Added loop ref and ignoring keypresses when window is not active