4
I have a page that has a header. I would like when clicking the F11 button (which leaves the browser in Full Screen mode) the header to be hidden from the page and when I click again, the header would reappear on the page.
When I first click on the F11 key, everything happens perfectly, the page enters full screen mode already without the header. However, when I click the F11 key again, the page leaves full screen mode, but the header does not return to the page. If I press F11 again after that, the page enters full screen mode with the header.
That is, the event onkeydown is not working in full screen.
I made a simple example here that elucidates the situation I’m going through with my code (The so-called minimal, complete and verifiable example). Look at:
<!DOCTYPE html>
<html lang="pt-BR">
<head>
</head>
<h1>Katie Bouman - Minha Namorada</h1>
<body>
<script>
document.onkeydown = function() {
let codigo_unicode_tecla = event.keyCode;
console.log(codigo_unicode_tecla);
if (codigo_unicode_tecla == '122') {
alert('Funcionou o click no F11');
}
}
</script>
</body>
</html>
- On my page, I simply check if the HTML element has the d-None class (from Bootstrap, which hides an element) at the moment of clicking the F11 key. If the element does not have such a class, then I add it with classList.add. If it does, I removed it with classList.remove. This is the dynamic of the code. Thanks in advance!
Have you tried calling
control+f11
orcommand+f11
and pass event by parameter in the function?document.onkeydown = function(event) { ... }
– Ivan Ferrer
@Ivanferrer this is exactly the code of the question, the problem only occurs in Chrome. In Msedge and Firefox works normally.
– Guilherme Nascimento
It would not be better to make a button to call a function for these events instead of using the key, hence you can call this function with the key too, and nor need to worry about onkeydown event?
– Ivan Ferrer
@Ivanferrer maybe he wants to detect independently of the form, just because there is no way to know what the user will use, the fullscreenchange event for example does not work with F11
– Guilherme Nascimento
Hello Schrödinger Cat, updated the answer, if you do not need to run in old browsers there is a
@media (display-mode: fullscreen)
, it works both when it enters fullscreen with F11 and withrequestFullscreen
, I just passed by to warn you, so it will decrease your need for Javascript by being able to better control the style directly in CSS.– Guilherme Nascimento
Thank you for the effort and for remembering, @Guilhermenascimento. I am grateful!
– Gato de Schrödinger