Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

142
Visualizações
Is there a way to tell if a canvas has focus?

I have a canvas running a game. You can move around in said game using wasd and arrow keys and such. To the left of it, there are some settings in HTML which when edited make live changes to the game. The problem is, when typing in the settings areas, the canvas still takes key inputs. So, for example, if I were to type in "points" in the settings area, the canvas would capture the "s" and move backwards in the game. It would still be typed into the settings area, though.

The solution I have come up with is to cancel all key inputs to the canvas if the canvas does not have focus.

I have tried using document.activeElement!==canvas, and that did not work. It made the canvas not accept key inputs whether it had focus or not.

I also tried sticking everything else in a div with an id of "notcanvas", using document.activeElement==notcanvas, which did not work either. It made the canvas always accept key inputs, regardless of if it had focus or not.

Earlier in the development of the "settings HTML area", I had had one large text area, as of which I would input some text. Checking if that one textarea had focus worked, but now I am using multiple textareas and form inputs, so having each one of those exclusively in the focus checking is not a good solution, as I may add more to it, and don't want to go back to it each time to add yet another singular HTML element to check.

So, is there any short way to check if a canvas has focus? Is a canvas even able to have focus?

The link to the project with the problem is here: https://replit.com/@KittyCraft0/3D-modeling-software-5#script.js:46:32

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

!notcanvas.contains(document.activeElement); works.

Although it may not work in internet explorer, according to mozilla docs: https://developer.mozilla.org/en-US/docs/Web/API/Node/contains

But I am already using object oriented programming, which I think I have read somehwere internet explorer is also not compatible with.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda