Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Keyboard Lock API support (Chromium 68+ only) #118

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion app/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import "core-js/stable";
import "regenerator-runtime/runtime";
import * as Log from '../core/util/logging.js';
import _, { l10n } from './localization.js';
import { isTouchDevice, isSafari, hasScrollbarGutter, dragThreshold, supportsBinaryClipboard, isFirefox, isWindows, isIOS, supportsPointerLock }
import { isTouchDevice, isSafari, hasScrollbarGutter, dragThreshold, supportsBinaryClipboard, isFirefox, isWindows, isIOS, supportsPointerLock, supportsKeyboardLock }
from '../core/util/browser.js';
import { setCapture, getPointerEvent } from '../core/util/events.js';
import KeyTable from "../core/input/keysym.js";
Expand Down Expand Up @@ -1841,6 +1841,9 @@ const UI = {
document.mozFullScreenElement || // currently working methods
document.webkitFullscreenElement ||
document.msFullscreenElement) {
if (supportsKeyboardLock) {
navigator.keyboard.unlock();
}
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.mozCancelFullScreen) {
Expand All @@ -1860,6 +1863,12 @@ const UI = {
} else if (document.body.msRequestFullscreen) {
document.body.msRequestFullscreen();
}
// No need to explicitly ask for permission,
// but it's expected that user grant it since Chromium 131.
// See https://developer.chrome.com/blog/keyboard-lock-pointer-lock-permission
if (supportsKeyboardLock) {
navigator.keyboard.lock();
}
}
UI.updateFullscreenButton();
},
Expand Down
3 changes: 2 additions & 1 deletion core/util/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ window.addEventListener('touchstart', function onFirstTouch() {
// brings us a bit closer but is not optimal.
export let dragThreshold = 10 * (window.devicePixelRatio || 1);

export const supportsKeyboardLock = ('keyboard' in navigator && 'lock' in navigator.keyboard && typeof(navigator.keyboard.lock) === 'function');

let _supportsCursorURIs = false;

try {
Expand Down Expand Up @@ -174,4 +176,3 @@ export function supportsPointerLock() {
if (isIOS() || isIE()) { return false; }
return (document.exitPointerLock);
}