Last active
August 25, 2017 14:38
-
-
Save trustedtomato/02060a3c6e160463fdfe86ecab4b45b9 to your computer and use it in GitHub Desktop.
Wait for a key in JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Wait for the given key; if key is omitted, any key will trigger. It resolves the event. */ | |
function waitForKey(key){ | |
return new Promise(resolve => { | |
var onkeydown = e => { | |
if(typeof key === 'undefined' || e.key === key){ | |
document.removeEventListener('keydown', onkeydown); | |
resolve(e); | |
} | |
}; | |
document.addEventListener('keydown', onkeydown); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment