Skip to content

Commit

Permalink
Track time to prevent erroneous shortcut repeats (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
EddieAbbondanzio authored Dec 25, 2022
1 parent 27c5d36 commit 0ad6730
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/renderer/io/shortcuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import { Section } from "../../shared/ui/app";
import { log } from "../logger";
import { BrowserWindowEvent, IpcChannel } from "../../shared/ipc";

const INITIAL_DELAY_MS = 400;
const REPEAT_DELAY_MS = 200;
const INITIAL_DELAY_MS = 300;
const REPEAT_DELAY_MS = 150;

export function useShortcuts(store: Store): void {
const { dispatch, state } = store;
Expand All @@ -26,6 +26,7 @@ export function useShortcuts(store: Store): void {

// We use useState because we want to trigger a re-render if the keys change.
const [didKeysChange, setDidKeysChange] = useState(false);
const lastTriggerTime = useRef(Date.now());

if (!isTest() && shortcuts.length === 0) {
void log.info("No shortcuts were passed to the useShortcuts hook");
Expand All @@ -51,6 +52,12 @@ export function useShortcuts(store: Store): void {
if (shortcut != null) {
void dispatch(shortcut.event as UIEventType, shortcut.eventInput);

// Track time of last shortcut trigger to prevent repeat shortcuts from
// being triggered multiple times if the shortcut was pressed, released, and
// pressed again before the repeat interval fires.
const currTime = Date.now();
lastTriggerTime.current = currTime;

if (shortcut.repeat) {
void (async () => {
const keysStarted = activeKeysToArray(activeKeys.current);
Expand All @@ -63,7 +70,10 @@ export function useShortcuts(store: Store): void {

const trigger = () => {
const keysOnInterval = activeKeysToArray(activeKeys.current);
if (isEqual(keysStarted, keysOnInterval)) {
if (
isEqual(keysStarted, keysOnInterval) &&
lastTriggerTime.current === currTime
) {
void dispatch(shortcut.event as UIEventType, shortcut.eventInput);
}
};
Expand Down Expand Up @@ -95,6 +105,7 @@ export function useShortcuts(store: Store): void {
};
}, []);

// Subscribe to window events
useEffect(() => {
const keyDown = (ev: KeyboardEvent) => {
// Prevent redundant calls
Expand Down

0 comments on commit 0ad6730

Please sign in to comment.