From d28df77dac1bcb96c90e25ad2ea2c210e8b8294c Mon Sep 17 00:00:00 2001 From: pacien Date: Fri, 5 Jan 2024 00:26:39 +0100 Subject: record when holding space key --- app.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'app.js') diff --git a/app.js b/app.js index 762a72f..19d0867 100644 --- a/app.js +++ b/app.js @@ -44,7 +44,6 @@ function onGetDeviceSuccess(stream) { clips.prepend(wrapElement("li", audioElement)); }); - // TODO: handle "space" key hold the same as holding the "record" button recordBtn.addEventListener("mousedown", _event => { mediaRecorder.start(); }); @@ -52,6 +51,20 @@ function onGetDeviceSuccess(stream) { recordBtn.addEventListener("mouseup", _event => { mediaRecorder.stop(); }); + + document.addEventListener("keydown", event => { + if (mediaRecorder.state == "inactive" && event.key == " ") { + event.preventDefault(); // prevent scroll + mediaRecorder.start(); + } + }); + + document.addEventListener("keyup", event => { + if (event.key == " ") { + event.preventDefault(); // prevent scroll + mediaRecorder.stop(); + } + }); } function onGetDeviceError(error) { -- cgit v1.2.3