Login  Register

Re: NewtCanvasJFX not giving up focus

Posted by Taylor on Jun 16, 2020; 4:56pm
URL: https://forum.jogamp.org/NewtCanvasJFX-not-giving-up-focus-tp4040705p4040710.html

I found a bad hack that allows it to work but there has got to be a better way. Essentially I listen to the real GLWindow for when it gains focus and tell Javafx to also give focus to something (glCanvas in my case). Then when glCanvas loses focus I quickly flip-flop visibility of the window which makes it give up focus.

Code wrote
        ...

        val window = GLWindow.create(screen, glCaps)
        val glCanvas = NewtCanvasJFX(window)

        window.addWindowListener(object : WindowAdapter() {
            override fun windowGainedFocus(e: WindowEvent) {
                // when heavyweight window gains focus, also tell javafx to give focus to glCanvas
                glCanvas.requestFocus()
            }
        })

        glCanvas.focusedProperty().addListener { _, _, newValue ->
            if (!newValue) {
                JFXAccessor.runOnJFXThread(false) {
                    window.isVisible = false
                    window.isVisible = true
                }
            }
        }