Skip to content

🎥 Make videos programmatically with React

implement ability to disable keyboard shortcuts

Jonny Burger52e099f761a9
diff --git a/packages/bundler/src/webpack-config.ts b/packages/bundler/src/webpack-config.ts
index 28b8f7beac..a144d23916 100644
--- a/packages/bundler/src/webpack-config.ts
+++ b/packages/bundler/src/webpack-config.ts
@@ -45,6 +45,7 @@ export const webpackConfig = ({
maxTimelineTracks,
entryPoints,
remotionRoot,
+ keyboardShortcutsEnabled,
}: {
entry: string;
userDefinedComponent: string;
@@ -55,6 +56,7 @@ export const webpackConfig = ({
enableCaching?: boolean;
envVariables: Record<string, string>;
maxTimelineTracks: number;
+ keyboardShortcutsEnabled: boolean;
entryPoints: string[];
remotionRoot: string;
}): [string, WebpackConfiguration] => {
@@ -100,6 +102,8 @@ export const webpackConfig = ({
new webpack.HotModuleReplacementPlugin(),
new webpack.DefinePlugin({
'process.env.MAX_TIMELINE_TRACKS': maxTimelineTracks,
+ 'process.env.KEYBOARD_SHORTCUTS_ENABLED':
+ keyboardShortcutsEnabled,
[`process.env.${Internals.ENV_VARIABLES_ENV_NAME}`]:
JSON.stringify(envVariables),
}),
diff --git a/packages/cli/src/config/index.ts b/packages/cli/src/config/index.ts
index 4e2d942b24..226a9abb01 100644
--- a/packages/cli/src/config/index.ts
+++ b/packages/cli/src/config/index.ts
@@ -62,6 +62,10 @@ import {setFfmpegExecutable, setFfprobeExecutable} from './ffmpeg-executable';
import {setFrameRange} from './frame-range';
import {setImageFormat} from './image-format';
import {setImageSequence} from './image-sequence';
+import {
+ getKeyboardShortcutsEnabled,
+ setKeyboardShortcutsEnabled,
+} from './keyboard-shortcuts';
import {setLogLevel} from './log';
import {setMaxTimelineTracks} from './max-timeline-tracks';
import {getMuted, setMuted} from './muted';
@@ -81,6 +85,7 @@ import {setWebpackCaching} from './webpack-caching';
export const Config: ConfigType = {
Preview: {
setMaxTimelineTracks,
+ setKeyboardShortcutsEnabled,
},
Bundling: {
overrideWebpackConfig,
@@ -166,6 +171,7 @@ export const ConfigInternals = {
getMuted,
getEnforceAudioTrack,
setEnforceAudioTrack,
+ getKeyboardShortcutsEnabled,
};
export const overrideRemotion = () => {
diff --git a/packages/cli/src/config/keyboard-shortcuts.ts b/packages/cli/src/config/keyboard-shortcuts.ts
new file mode 100644
index 0000000000..afc3c24c90
--- /dev/null
+++ b/packages/cli/src/config/keyboard-shortcuts.ts
@@ -0,0 +1,9 @@
+let shortcutsEnabled = true;
+
+export const setKeyboardShortcutsEnabled = (enabled: boolean) => {
+ shortcutsEnabled = enabled;
+};
+
+export const getKeyboardShortcutsEnabled = () => {
+ return shortcutsEnabled;
+};
diff --git a/packages/cli/src/editor/components/CheckboardToggle.tsx b/packages/cli/src/editor/components/CheckboardToggle.tsx
index fb2440a2e3..decc544901 100644
--- a/packages/cli/src/editor/components/CheckboardToggle.tsx
+++ b/packages/cli/src/editor/components/CheckboardToggle.tsx
@@ -1,8 +1,15 @@
import React, {useCallback, useContext} from 'react';
+import {truthy} from '../../truthy';
+import {areKeyboardShortcutsDisabled} from '../helpers/use-keybinding';
import {CheckerboardContext} from '../state/checkerboard';
import {ControlButton} from './ControlButton';
-const accessibilityLabel = 'Show transparency as checkerboard (T)';
+const accessibilityLabel = [
+ 'Show transparency as checkerboard',
+ areKeyboardShortcutsDisabled() ? null : '(T)',
+]
+ .filter(truthy)
+ .join(' ');
export const CheckboardToggle: React.FC = () => {
const {checkerboard, setCheckerboard} = useContext(CheckerboardContext);
diff --git a/packages/cli/src/editor/components/KeyboardShortcutsModal.tsx b/packages/cli/src/editor/components/KeyboardShortcutsModal.tsx
index 60944640f4..d01b91332f 100644
--- a/packages/cli/src/editor/components/KeyboardShortcutsModal.tsx
+++ b/packages/cli/src/editor/components/KeyboardShortcutsModal.tsx
@@ -1,4 +1,5 @@
import React, {useCallback, useContext} from 'react';
+import {areKeyboardShortcutsDisabled} from '../helpers/use-keybinding';
import {ArrowLeft, ArrowRight, ShiftIcon} from '../icons/keys';
import {ModalsContext} from '../state/modals';
import {Column, Row, Spacing} from './layout';
@@ -46,6 +47,22 @@ const title: React.CSSProperties = {
marginBottom: 10,
};
+const keyboardShortcutsDisabled: React.CSSProperties = {
+ padding: 12,
+ width: '100%',
+ fontSize: 14,
+ backgroundColor: 'rgba(255, 255, 255, 0.1)',
+};
+
+const ul: React.CSSProperties = {
+ marginTop: 0,
+ marginBottom: 0,
+};
+
+const li: React.CSSProperties = {
+ fontSize: 14,
+};
+
export const KeyboardShortcuts: React.FC = () => {
const {setSelectedModal} = useContext(ModalsContext);
@@ -56,6 +73,18 @@ export const KeyboardShortcuts: React.FC = () => {
return (
<ModalContainer onEscape={onQuit} onOutsideClick={onQuit}>
<NewCompHeader title="Keyboard shortcuts" />
+ {areKeyboardShortcutsDisabled() ? (
+ <div style={keyboardShortcutsDisabled}>
+ Keyboard shortcuts disabled either due to:
+ <ul style={ul}>
+ <li style={li}>a) --disable-keyboard-shortcuts being passed</li>
+ <li style={li}>
+ b) Config.Preview.setKeyboardShortcutsEnabled(false) being set or
+ </li>
+ <li style={li}> c) a Remotion version mismatch.</li>
+ </ul>
+ </div>
+ ) : null}
<Row style={container}>
<Column>
<div style={title}>Playback</div>
diff --git a/packages/cli/src/editor/components/Menu/MenuSubItem.tsx b/packages/cli/src/editor/components/Menu/MenuSubItem.tsx
index c7902ffbc1..d1cebfee65 100644
--- a/packages/cli/src/editor/components/Menu/MenuSubItem.tsx
+++ b/packages/cli/src/editor/components/Menu/MenuSubItem.tsx
@@ -2,6 +2,7 @@ import {PlayerInternals} from '@remotion/player';
import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react';
import ReactDOM from 'react-dom';
import {CLEAR_HOVER, LIGHT_TEXT} from '../../helpers/colors';
+import {areKeyboardShortcutsDisabled} from '../../helpers/use-keybinding';
import {CaretRight} from '../../icons/caret';
import {useZIndex} from '../../state/z-index';
import {Flex, Row, Spacing} from '../layout';
@@ -144,7 +145,9 @@ export const MenuSubItem: React.FC<{
<div style={labelStyle}>{label}</div> <Flex />
<Spacing x={2} />
{subMenu ? <CaretRight /> : null}
- {keyHint ? <span style={keyHintCss}>{keyHint}</span> : null}
+ {keyHint && !areKeyboardShortcutsDisabled() ? (
+ <span style={keyHintCss}>{keyHint}</span>
+ ) : null}
{portalStyle && subMenu
? ReactDOM.createPortal(
<SubMenuComponent
diff --git a/packages/cli/src/editor/components/MenuToolbar.tsx b/packages/cli/src/editor/components/MenuToolbar.tsx
index 3ada904b0d..3171930b0c 100644
--- a/packages/cli/src/editor/components/MenuToolbar.tsx
+++ b/packages/cli/src/editor/components/MenuToolbar.tsx
@@ -2,6 +2,7 @@ import React, {useCallback, useContext, useMemo, useState} from 'react';
import {Internals} from 'remotion';
import {BACKGROUND} from '../helpers/colors';
import {pickColor} from '../helpers/pick-color';
+import {areKeyboardShortcutsDisabled} from '../helpers/use-keybinding';
import {Checkmark} from '../icons/Checkmark';
import {CheckerboardContext} from '../state/checkerboard';
import {ModalsContext} from '../state/modals';
@@ -408,7 +409,9 @@ export const MenuToolbar: React.FC = () => {
{
id: 'shortcuts',
value: 'shortcuts',
- label: 'Shortcuts',
+ label: areKeyboardShortcutsDisabled()
+ ? 'Shortcuts (disabled)'
+ : 'Shortcuts',
onClick: () => {
close();
diff --git a/packages/cli/src/editor/components/TimelineInOutToggle.tsx b/packages/cli/src/editor/components/TimelineInOutToggle.tsx
index 39e31e3596..94d9a1746e 100644
--- a/packages/cli/src/editor/components/TimelineInOutToggle.tsx
+++ b/packages/cli/src/editor/components/TimelineInOutToggle.tsx
@@ -6,8 +6,12 @@ import React, {
useImperativeHandle,
} from 'react';
import {Internals} from 'remotion';
+import {truthy} from '../../truthy';
import {useIsStill} from '../helpers/is-current-selected-still';
-import {useKeybinding} from '../helpers/use-keybinding';
+import {
+ areKeyboardShortcutsDisabled,
+ useKeybinding,
+} from '../helpers/use-keybinding';
import {
TimelineInPointer,
TimelineOutPointer,
@@ -19,7 +23,10 @@ import {
import {persistMarks} from '../state/marks';
import {ControlButton} from './ControlButton';
-const getTooltipText = (pointType: string) => `Mark ${pointType}`;
+const getTooltipText = (pointType: string, key: string) =>
+ [`Mark ${pointType}`, areKeyboardShortcutsDisabled() ? null : `(${key})`]
+ .filter(truthy)
+ .join(' ');
const style: React.CSSProperties = {
width: 16,
@@ -215,8 +222,8 @@ export const TimelineInOutPointToggle: React.FC = () => {
return (
<>
<ControlButton
- title={getTooltipText('In (I)')}
- aria-label={getTooltipText('In (I)')}
+ title={getTooltipText('In', 'I')}
+ aria-label={getTooltipText('In', 'I')}
onClick={onInMark}
disabled={timelinePosition === 0}
>
@@ -226,8 +233,8 @@ export const TimelineInOutPointToggle: React.FC = () => {
/>
</ControlButton>
<ControlButton
- title={getTooltipText('Out (O)')}
- aria-label={getTooltipText('Out (O)')}
+ title={getTooltipText('Out', '(O)')}
+ aria-label={getTooltipText('Out', '(O)')}
onClick={onOutMark}
disabled={timelinePosition === videoConfig.durationInFrames - 1}
>
diff --git a/packages/cli/src/editor/helpers/use-keybinding.ts b/packages/cli/src/editor/helpers/use-keybinding.ts
index 8292aa4519..850c4ff71c 100644
--- a/packages/cli/src/editor/helpers/use-keybinding.ts
+++ b/packages/cli/src/editor/helpers/use-keybinding.ts
@@ -3,6 +3,16 @@ import type {KeyEventType, RegisteredKeybinding} from '../state/keybindings';
import {KeybindingContext} from '../state/keybindings';
import {useZIndex} from '../state/z-index';
+if (!process.env.KEYBOARD_SHORTCUTS_ENABLED) {
+ console.warn(
+ 'Keyboard shortcuts disabled either due to: a) --disable-keyboard-shortcuts being passed b) Config.Preview.setKeyboardShortcutsEnabled(false) being set or c) a Remotion version mismatch.'
+ );
+}
+
+export const areKeyboardShortcutsDisabled = () => {
+ return !process.env.KEYBOARD_SHORTCUTS_ENABLED;
+};
+
export const useKeybinding = () => {
const [paneId] = useState(() => String(Math.random()));
const context = useContext(KeybindingContext);
@@ -15,6 +25,12 @@ export const useKeybinding = () => {
commandCtrlKey: boolean;
callback: (e: KeyboardEvent) => void;
}) => {
+ if (!process.env.KEYBOARD_SHORTCUTS_ENABLED) {
+ return {
+ unregister: () => undefined,
+ };
+ }
+
if (!isHighestContext) {
return {
unregister: () => undefined,
diff --git a/packages/cli/src/get-cli-options.ts b/packages/cli/src/get-cli-options.ts
index ea02cbedb0..eea71a08af 100644
--- a/packages/cli/src/get-cli-options.ts
+++ b/packages/cli/src/get-cli-options.ts
@@ -275,5 +275,6 @@ export const getCliOptions = async (options: {
port: port ?? null,
muted: ConfigInternals.getMuted(),
enforceAudioTrack: ConfigInternals.getEnforceAudioTrack(),
+ keyboardShortcutsEnables: ConfigInternals.getKeyboardShortcutsEnabled(),
};
};
diff --git a/packages/cli/src/parse-command-line.ts b/packages/cli/src/parse-command-line.ts
index 61108a6493..ecdeb82157 100644
--- a/packages/cli/src/parse-command-line.ts
+++ b/packages/cli/src/parse-command-line.ts
@@ -47,6 +47,7 @@ export type CommandLineOptions = {
port: number;
frame: string | number;
['disable-headless']: boolean;
+ ['disable-keyboard-shortcuts']: boolean;
muted: boolean;
['enforce-audio-track']: boolean;
gl: OpenGlRenderer;
@@ -71,6 +72,7 @@ export const BooleanFlags = [
'disable-web-security',
'ignore-certificate-errors',
'disable-headless',
+ 'disable-keyboard-shortcuts',
];
export const parsedCli = minimist<CommandLineOptions>(process.argv.slice(2), {
@@ -222,6 +224,12 @@ export const parseCommandLine = (
Config.Rendering.setMuted(parsedCli.muted);
}
+ if (typeof parsedCli['disable-keyboard-shortcuts'] !== 'undefined') {
+ Config.Preview.setKeyboardShortcutsEnabled(
+ !parsedCli['disable-keyboard-shortcuts']
+ );
+ }
+
if (typeof parsedCli['enforce-audio-track'] !== 'undefined') {
Config.Rendering.setEnforceAudioTrack(parsedCli['enforce-audio-track']);
}
diff --git a/packages/cli/src/preview-server/start-server.ts b/packages/cli/src/preview-server/start-server.ts
index f45c063bc3..79188d4752 100644
--- a/packages/cli/src/preview-server/start-server.ts
+++ b/packages/cli/src/preview-server/start-server.ts
@@ -24,6 +24,7 @@ export const startServer = async (
port: number | null;
maxTimelineTracks?: number;
remotionRoot: string;
+ keyboardShortcutsEnabled: boolean;
}
): Promise<{
port: number;
@@ -47,6 +48,7 @@ export const startServer = async (
require.resolve('./error-overlay/entry-basic.js'),
],
remotionRoot: options.remotionRoot,
+ keyboardShortcutsEnabled: options.keyboardShortcutsEnabled,
});
const compiler = webpack(config);
diff --git a/packages/cli/src/preview.ts b/packages/cli/src/preview.ts
index 6183f44601..b96c980fd9 100644
--- a/packages/cli/src/preview.ts
+++ b/packages/cli/src/preview.ts
@@ -71,6 +71,7 @@ export const previewCommand = async (remotionRoot: string) => {
port: desiredPort,
maxTimelineTracks: ConfigInternals.getMaxTimelineTracks(),
remotionRoot,
+ keyboardShortcutsEnabled: ConfigInternals.getKeyboardShortcutsEnabled(),
}
);
diff --git a/packages/core/src/config.ts b/packages/core/src/config.ts
index 82d3b9a4d3..2d75213c3d 100644
--- a/packages/core/src/config.ts
+++ b/packages/core/src/config.ts
@@ -35,6 +35,12 @@ export type ConfigType = {
* @default 15
*/
readonly setMaxTimelineTracks: (maxTracks: number) => void;
+ /**
+ * Enable Keyboard shortcuts in the Remotion Preview.
+ * @param enabled Boolean whether to enable the keyboard shortcuts
+ * @default true
+ */
+ readonly setKeyboardShortcutsEnabled: (enableShortcuts: boolean) => void;
};
readonly Bundling: {
/**