From cae43ec998b6e029d61dbb03119b3541b52ea68e Mon Sep 17 00:00:00 2001 From: Serraniel Date: Wed, 29 Jul 2020 13:49:05 +0200 Subject: [PATCH] #12 Extended helpers for pressed State of shift and ctrl keys --- utils/helpers.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/utils/helpers.js b/utils/helpers.js index cb18055..ed73b4f 100644 --- a/utils/helpers.js +++ b/utils/helpers.js @@ -1,3 +1,25 @@ +var isShiftPressed = false; +var isCtrlPressed = false; + function isHtmlElement(object) { return object instanceof HTMLElement; +} + +document.addEventListener('keydown', event => handleKeyDown(event)); +document.addEventListener('keyup', event => handleKeyUp(event)); + +function handleKeyDown(event) { + handleKeyToggle(event, true); +} + +function handleKeyUp(event) { + handleKeyToggle(event, false); +} + +function handleKeyToggle(event, isPressed) { + if (event.key === 'Shift') { + isShiftPressed = isPressed; + } else if (event.key === 'Control') { + isCtrlPressed = isPressed; + } } \ No newline at end of file