5

I've got a very strange behavior of the keyUp event. JS snippet is attached. Tested on Chrome/Chromium.

Steps to reproduce:

  1. F11 to go fullscreen
  2. Press down A key (leave it pressed)
  3. Press F11 to exit fullscreen
  4. Release the A key

Result - NO output lines in the console! Why is that? I expected to see something, but it seems that exiting a fullscreen disables the future keyUp events. In my case I start a job by pressing a key, and need to stop it after the release. Currently it continues to run.

The question is - how do I get it working?

Could that be a Chrome/Chromium bug? I've tried Firefox now - keyup event is successfully triggered.

i = 0;

window.addEventListener("keyup", function() {
    console.log("key up, " + i);
    i = i+1;
});
Alexey
  • 2,582
  • 3
  • 13
  • 31

1 Answers1

0

It doesn't work in chrome but it does in firefox. I guess it depends on how the browser is handling the keypress event when f11 is pressed. Maybe chrome is using preventDefault on the event. This answer here explains why it does that:

Chrome eats javascript keydown event handler on F11 key press, when browser is already in full screen mode

darkknight
  • 216
  • 5
  • 15
  • What can I do then? In my case I start a job by pressing a key, and need to stop it after the release. Currently it continues to run – Alexey Aug 14 '18 at 19:18
  • You can use fullscreenchange event which is fired in chrome when full screen changes as explained here: https://stackoverflow.com/questions/25126106/capture-esc-event-when-exiting-full-screen-mode – darkknight Aug 14 '18 at 19:22
  • I need to call a JS function after the releasing a key, not right after exiting a fullscreen – Alexey Aug 14 '18 at 19:32
  • As explained, due to security reason, chrome wants to give full control to the user if the user wants to exit out of full screen. So it cancels all the events and gives the control to the user. Since keyup is a cancelable event, it is cancelled. So you lose the event whenever you press f11. Sadly you can do nothing about it. – darkknight Aug 14 '18 at 19:47
  • Ok, thanks for explanations. I see that **fullscreenchange** also doesn't work with **F11**. So things are bad with this?! – Alexey Aug 14 '18 at 20:07
  • It will work but you won't be able to achieve what you want – darkknight Aug 14 '18 at 20:08
  • Please show how. I have a workaround idea, if I can get "fullscreen exit" event in JS – Alexey Aug 14 '18 at 20:14