From 8012ce0c572ee26e09c3ba6b470865c59c3b13d0 Mon Sep 17 00:00:00 2001 From: mesca Date: Sun, 9 Oct 2022 16:06:51 +0200 Subject: [PATCH] Add key press awaitable --- timeflux_ui/www/common/assets/js/timeflux.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/timeflux_ui/www/common/assets/js/timeflux.js b/timeflux_ui/www/common/assets/js/timeflux.js index 22503a5..1dc8a88 100644 --- a/timeflux_ui/www/common/assets/js/timeflux.js +++ b/timeflux_ui/www/common/assets/js/timeflux.js @@ -478,6 +478,24 @@ function sleep(duration) { }); } +/** + * Resolve promise on keypress + * + * @param {number} code - key code + * @returns {Promise} + */ +function key(code) { + return new Promise((resolve) => { + const handler = (event) => { + if (code === undefined || event.keyCode === code) { + document.removeEventListener(event.type, handler); + resolve(true); + } + }; + document.addEventListener('keydown', handler); + }); +} + /** * Load settings *