diff --git a/src/components/KeyboardShortcuts.jsx b/src/components/KeyboardShortcuts.jsx index acbbb93..9f5109d 100644 --- a/src/components/KeyboardShortcuts.jsx +++ b/src/components/KeyboardShortcuts.jsx @@ -1,6 +1,6 @@ -import React, { memo, Fragment, useEffect, useMemo, useCallback, useState } from 'react'; +import React, { memo, Fragment, useEffect, useMemo, useCallback, useState, useRef } from 'react'; import { useTranslation } from 'react-i18next'; -import { InlineAlert, UndoIcon, Paragraph, TakeActionIcon, IconButton, Button, DeleteIcon, AddIcon, Heading, Text, Dialog } from 'evergreen-ui'; +import { PlusIcon, InlineAlert, UndoIcon, Paragraph, TakeActionIcon, IconButton, Button, DeleteIcon, AddIcon, Heading, Text, Dialog } from 'evergreen-ui'; import { FaMouse, FaPlus, FaStepForward, FaStepBackward } from 'react-icons/fa'; import Mousetrap from 'mousetrap'; import groupBy from 'lodash/groupBy'; @@ -47,16 +47,20 @@ const CreateBinding = memo(({ const isShown = action != null; useEffect(() => { - if (isShown) setKeysDown([]); + if (isShown) { + setKeysDown([]); + } }, [isShown]); + const addKeyDown = useCallback((character) => setKeysDown((old) => [...new Set([...old, character])]), []); + useEffect(() => { if (!isShown) return undefined; const mousetrap = new Mousetrap(); function handleKey(character, modifiers, e) { if (['keydown', 'keypress'].includes(e.type)) { - setKeysDown((old) => [...new Set([...old, character])]); + addKeyDown(character); } e.preventDefault(); } @@ -66,7 +70,7 @@ const CreateBinding = memo(({ return () => { mousetrap.handleKey = handleKeyOrig; }; - }, [isShown]); + }, [addKeyDown, isShown]); const isComboInvalid = validKeysDown.length === 0 && keysDown.length > 0; @@ -80,7 +84,7 @@ const CreateBinding = memo(({ onConfirm={() => onNewKeyBindingConfirmed(action, keysDown)} onCancel={() => setCreatingBinding()} > - {action ? ( + {isShown ? (
{actionsMap[action].name} ({action}) @@ -90,7 +94,10 @@ const CreateBinding = memo(({ {isComboInvalid && {t('Combination is invalid')}} - {keysDown.length > 0 &&
} +
+ {!keysDown.includes('esc') && } + {keysDown.length > 0 && } +
) :
}