0

I have an listener which does some action when a mousenter event happens and ctrl key is pressed. so e.g

somenode.addEventListener(events.EventType.MOUSEENTER, event => {  
          if (event.ctrlKey) {
            // do action_1
          }
        });

I also have to do some other action when the ctrl key is released. so e.g

 document.addEventListener('keyup', (e) => {
      if (e.keyCode == KeyCodes.CTRL) {
        // do action_2
      }
    });

this works fine. The problem happens when lets say i open chrome console, the document losses focus. Now when the user points back the mouse over to the page somehow the mouseenter event still happens (even when page is not in foucs) however if they release the ctrl key i do not get that call back (possibly because the page is not in focus).

This kinds of break the flow as i am only able to do action_1 . Is there a way i can figure out when the ctrl key is released so that i can also perform action_2 ?

root
  • 3,517
  • 2
  • 19
  • 25
  • you could use the mouseover event to put the page back in focus. Probably need some logic to prevent focus from being moved while the page is being used, only when the focus on page has been lost. – async await Aug 20 '21 at 23:46
  • thanks for the suggestion. I tried adding sth like document.body.focus() , but it didnt work. Any suggestion how can i put it back in focus ? – root Aug 21 '21 at 15:40
  • I believe I misunderstood the issue. We can not force the user back into the browser from another location outside the page due to security. If something else has the keyboard's focus, your page can not call it. Feel free to review this [related answer](https://stackoverflow.com/questions/8135188/focus-tab-or-window) – async await Aug 21 '21 at 22:29

0 Answers0