Skip to content

Commit

Permalink
fix(player): add separate key up and down callbacks to shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
mihar-22 committed Feb 15, 2024
1 parent cfe2331 commit b643602
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
18 changes: 16 additions & 2 deletions packages/vidstack/src/core/keyboard/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,14 @@ export class MediaKeyboardController extends MediaPlayerController {
let { method, value } = this._getMatchingMethod(event);

if (!isString(value) && !isArray(value)) {
value?.callback(event, this._media.remote);
value?.onKeyUp?.({
event,
player: this._media.player,
remote: this._media.remote,
});

value?.callback?.(event, this._media.remote);

return;
}

Expand Down Expand Up @@ -118,7 +125,14 @@ export class MediaKeyboardController extends MediaPlayerController {
isNumberPress = !event.metaKey && /^[0-9]$/.test(event.key);

if (!isString(value) && !isArray(value) && !isNumberPress) {
value?.callback(event, this._media.remote);
value?.onKeyDown?.({
event,
player: this._media.player,
remote: this._media.remote,
});

value?.callback?.(event, this._media.remote);

return;
}

Expand Down
17 changes: 16 additions & 1 deletion packages/vidstack/src/core/keyboard/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { MediaPlayer } from '../../components/player';
import type { MediaRemoteControl } from '../state/remote-control';

export type MediaKeyTarget = 'document' | 'player';
Expand All @@ -21,5 +22,19 @@ export type MediaKeyShortcut = MediaKeysCallback | string | string[] | null;

export interface MediaKeysCallback {
keys: string | string[];
callback(event: KeyboardEvent, remote: MediaRemoteControl): void;

/** @deprecated - use `onKeyUp` or `onKeyDown` */
callback?(event: KeyboardEvent, remote: MediaRemoteControl): void;

onKeyUp?(context: {
event: KeyboardEvent;
player: MediaPlayer;
remote: MediaRemoteControl;
}): void;

onKeyDown?(context: {
event: KeyboardEvent;
player: MediaPlayer;
remote: MediaRemoteControl;
}): void;
}

0 comments on commit b643602

Please sign in to comment.