2023-01-12 17:31:19 +00:00
|
|
|
/*
|
|
|
|
Copyright 2022-2023 New Vector Ltd
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2023-04-19 15:17:32 -04:00
|
|
|
import { useCallback, useRef } from "react";
|
2023-01-12 17:31:19 +00:00
|
|
|
|
|
|
|
import { getSetting } from "./settings/useSetting";
|
|
|
|
import { useEventTarget } from "./useEvents";
|
|
|
|
|
2023-01-13 11:52:40 +00:00
|
|
|
export function useCallViewKeyboardShortcuts(
|
2023-01-12 17:31:19 +00:00
|
|
|
enabled: boolean,
|
|
|
|
toggleMicrophoneMuted: () => void,
|
|
|
|
toggleLocalVideoMuted: () => void,
|
|
|
|
setMicrophoneMuted: (muted: boolean) => void
|
|
|
|
) {
|
2023-04-19 15:17:32 -04:00
|
|
|
const spacebarHeld = useRef(false);
|
2023-01-12 17:31:19 +00:00
|
|
|
|
|
|
|
useEventTarget(
|
|
|
|
window,
|
|
|
|
"keydown",
|
|
|
|
useCallback(
|
|
|
|
(event: KeyboardEvent) => {
|
|
|
|
if (!enabled) return;
|
|
|
|
// Check if keyboard shortcuts are enabled
|
|
|
|
const keyboardShortcuts = getSetting("keyboard-shortcuts", true);
|
|
|
|
if (!keyboardShortcuts) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.key === "m") {
|
|
|
|
toggleMicrophoneMuted();
|
|
|
|
} else if (event.key == "v") {
|
|
|
|
toggleLocalVideoMuted();
|
2023-04-19 15:17:32 -04:00
|
|
|
} else if (event.key === " " && !spacebarHeld.current) {
|
|
|
|
spacebarHeld.current = true;
|
2023-01-12 17:31:19 +00:00
|
|
|
setMicrophoneMuted(false);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[
|
|
|
|
enabled,
|
2023-01-12 18:26:21 +00:00
|
|
|
spacebarHeld,
|
2023-01-12 17:31:19 +00:00
|
|
|
toggleLocalVideoMuted,
|
|
|
|
toggleMicrophoneMuted,
|
|
|
|
setMicrophoneMuted,
|
|
|
|
]
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
useEventTarget(
|
|
|
|
window,
|
|
|
|
"keyup",
|
|
|
|
useCallback(
|
|
|
|
(event: KeyboardEvent) => {
|
|
|
|
if (!enabled) return;
|
|
|
|
// Check if keyboard shortcuts are enabled
|
|
|
|
const keyboardShortcuts = getSetting("keyboard-shortcuts", true);
|
|
|
|
if (!keyboardShortcuts) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.key === " ") {
|
2023-04-19 15:17:32 -04:00
|
|
|
spacebarHeld.current = false;
|
2023-01-12 17:31:19 +00:00
|
|
|
setMicrophoneMuted(true);
|
|
|
|
}
|
|
|
|
},
|
2023-04-19 15:17:32 -04:00
|
|
|
[enabled, setMicrophoneMuted]
|
2023-01-12 17:31:19 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
useEventTarget(
|
|
|
|
window,
|
|
|
|
"blur",
|
|
|
|
useCallback(() => {
|
|
|
|
if (spacebarHeld) {
|
2023-04-19 15:17:32 -04:00
|
|
|
spacebarHeld.current = false;
|
2023-01-12 17:31:19 +00:00
|
|
|
setMicrophoneMuted(true);
|
|
|
|
}
|
2023-04-19 15:17:32 -04:00
|
|
|
}, [setMicrophoneMuted, spacebarHeld])
|
2023-01-12 17:31:19 +00:00
|
|
|
);
|
|
|
|
}
|