Skip to content

Commit

Permalink
Merge pull request m1k1o#232 from Dishwasha/master
Browse files Browse the repository at this point in the history
Use Keyboard API to lock screen for supported browsers
  • Loading branch information
m1k1o authored Jan 7, 2023
2 parents 1c228a7 + 10e2fbd commit 1509521
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 1509521

Please sign in to comment.