Skip to content

Commit

Permalink
Use Keyboard API to lock screen for supported browsers
Browse files Browse the repository at this point in the history
When using fullscreen mode, hitting the escape key will exit out of
fullscreen mode.  To stay in fullscreen mode but pass the escape
key through, we can use the Keyboard API to require the escape key
be held down https://wicg.github.io/keyboard-lock/#escape-key.

Tested in Chrome which supports Keyboard API and Firefox which does
not https://caniuse.com/?search=navigator.keyboard with no errors
in either case.
  • Loading branch information
Dishwasha committed Jan 7, 2023
1 parent 1c228a7 commit 10e2fbd
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
3 changes: 2 additions & 1 deletion client/src/components/video.vue
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@
<script lang="ts">
import { Component, Ref, Watch, Vue, Prop } from 'vue-property-decorator'
import ResizeObserver from 'resize-observer-polyfill'
import { elementRequestFullscreen, onFullscreenChange, isFullscreen } from '~/utils'
import { elementRequestFullscreen, onFullscreenChange, isFullscreen, lockKeyboard, unlockKeyboard } from '~/utils'
import Emote from './emote.vue'
import Resolution from './resolution.vue'
Expand Down Expand Up @@ -417,6 +417,7 @@
onFullscreenChange(this._player, () => {
this.fullscreen = isFullscreen()
this.fullscreen ? lockKeyboard() : unlockKeyboard()
this.onResize()
})
Expand Down
15 changes: 15 additions & 0 deletions client/src/types/navigator.keyboard.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// navigator.keyboard.d.ts

// Type declarations for Keyboard API
// https://developer.mozilla.org/en-US/docs/Web/API/Keyboard_API
interface Keyboard {
lock(keyCodes?: array<string>): Promise<void>;
unlock(): void;
}

interface NavigatorKeyboard {
// Only available in a secure context.
readonly keyboard?: Keyboard;
}

interface Navigator extends NavigatorKeyboard {}
12 changes: 12 additions & 0 deletions client/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ export function makeid(length: number) {
return result
}

export function lockKeyboard() {
if (navigator && navigator.keyboard) {
navigator.keyboard.lock();
}
}

export function unlockKeyboard() {
if (navigator && navigator.keyboard) {
navigator.keyboard.unlock();
}
}

export function elementRequestFullscreen(el: HTMLElement) {
if (typeof el.requestFullscreen === 'function') {
el.requestFullscreen()
Expand Down

0 comments on commit 10e2fbd

Please sign in to comment.