Никак, наверное. При выходе из fullscreen'а событие клавиатуры не передается документу.
Я себе такой стенд накидал:
Fullscreen mode:
<button onclick="openFullscreen();">On</button>
<button onclick="closeFullscreen();">Off</button>
<div>
<pre id="log" style="max-height: 300px"></pre>
</div>
<script>
const elem = document.documentElement;
/* Function to open fullscreen mode */
function openFullscreen() {
if (elem.requestFullscreen) {
elem.requestFullscreen();
} else if (elem.mozRequestFullScreen) { /* Firefox */
elem.mozRequestFullScreen();
} else if (elem.webkitRequestFullscreen) { /* Chrome, Safari & Opera */
elem.webkitRequestFullscreen();
} else if (elem.msRequestFullscreen) { /* IE/Edge */
elem = window.top.document.body; //To break out of frame in IE
elem.msRequestFullscreen();
}
}
/* Function to close fullscreen mode */
function closeFullscreen() {
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.mozCancelFullScreen) {
document.mozCancelFullScreen();
} else if (document.webkitExitFullscreen) {
document.webkitExitFullscreen();
} else if (document.msExitFullscreen) {
window.top.document.msExitFullscreen();
}
}
const isFullscreenMode = () => Math.abs(window.innerHeight - screen.height) < 10;
const devtoolsIsOpen = (() => {
const devtools = function () { this.opened = false; };
devtools.toString = function () { this.opened = true; };
return () => {
console.log('%c', devtools);
return devtools.opened;
};
})();
const log = (() => {
const log = document.querySelector('#log');
return message => {
let lines = log.textContent.split('\n');
lines = lines.slice(Math.max(0, lines.length - 5));
lines.push(message);
log.textContent = lines.join('\n');
}
})();
</script>
<script>
let lastEscapeKeydownTime = 0;
const ESC_KEYCODE = 27
const events = [
'fullscreenchange',
'mozfullscreenchange',
'webkitfullscreenchange',
'msfullscreenchange',
];
events.forEach(function (eventName) {
document.addEventListener(eventName, function () {
const isFSMode = isFullscreenMode();
log('FSMode: ' + (isFullscreenMode() ? 'on' : 'off'));
if (!isFSMode && (new Date() - lastEscapeKeydownTime) / 1000 < 50) {
log('This is a reaction to the click of a Escape button.');
}
});
});
['keydown', 'keypress', 'keyup'].forEach(eventName => {
document.addEventListener(eventName, function (e) {
if (e.code !== 'Escape' && e.keyCode !== ESC_KEYCODE) {
return;
}
lastEscapeKeydownTime = +new Date();
log('escape');
});
});
</script>