NewtCanvasAWT + key bindings

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

NewtCanvasAWT + key bindings

rndfax
Hello, everyone!

I have jogl-2.2.4 on my system and I'm trying to use NewtCanvasAWT with Swing components. For my program I need hotkeys. In the example below pressed keys do not trigger action if focus is inside NewtCanvasAWT component - i.e. if I click somewhere in the window (not in NewtCanvasAWT) and then press key 'w' I will see my message "!!!!!!!!", but when I click mouse on NewtCanvasAWT and then press key 'w' I don't see anything.

Could you please help me this problem? How to trigger actions when the focus is inside NewtCanvasAWT?


import javax.media.opengl.*;

import com.jogamp.nativewindow.*;
import com.jogamp.newt.*;
import com.jogamp.newt.event.*;
import com.jogamp.newt.event.awt.*;
import com.jogamp.newt.awt.*;
import com.jogamp.newt.opengl.*;

import java.awt.*;
import javax.swing.*;

import java.awt.event.*;
import static javax.swing.JComponent.*;

public class Main {
        public static void main(String[] args) throws Exception {
                GLProfile glprofile = GLProfile.getDefault();
                GLCapabilities glcaps = new GLCapabilities(glprofile);
                GLWindow win = GLWindow.create(glcaps);
               
                JPanel jPanel1 = new JPanel();
                jPanel1.setLayout(new BorderLayout());
                jPanel1.add(new Button("north"), BorderLayout.NORTH);
                jPanel1.add(new Button("south"), BorderLayout.SOUTH);
                jPanel1.add(new Button("east"), BorderLayout.EAST);
                jPanel1.add(new Button("west"), BorderLayout.WEST);
                jPanel1.add(new NewtCanvasAWT(win), BorderLayout.CENTER);
               
                JFrame jFrame1 = new JFrame("Swing Parent JFrame");
                jFrame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                jFrame1.setContentPane(jPanel1);
               
                ActionMap am = jFrame1.getRootPane().getActionMap();
                InputMap im = jFrame1.getRootPane().getInputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
               
                im.put(KeyStroke.getKeyStroke('w'), "eraser");
                am.put("eraser", new AbstractAction() {
                        public void actionPerformed(ActionEvent e) {
                                System.out.println("!!!!!!!!");
                        }
                });
               
                   
                SwingUtilities.invokeAndWait(new Runnable() {
                   public void run() {
                       jFrame1.setSize(640, 480);
                       jFrame1.validate();
                       jFrame1.setVisible(true);
                   }
                });
        }
}
Reply | Threaded
Open this post in threaded view
|

Re: NewtCanvasAWT + key bindings

gouessej
Administrator
Hi

Please use JOGL 2.3.2. If you find a bug, we won't be able to help you if you don't use the latest version.

Do you have the same problem if you replace NewtCanvasAWT by java.awt.Canvas?
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: NewtCanvasAWT + key bindings

rndfax
Hello!

> Please use JOGL 2.3.2. If you find a bug, we won't be able to help you if you don't use the latest version.

Ok, I'm using it now - downloaded from http://jogamp.org/deployment/jogamp-current/archive/Sources/ and built it.

> Do you have the same problem if you replace NewtCanvasAWT by java.awt.Canvas?

No, with java.awt.Canvas everything is fine. Below you can see the code for java.awt.Canvas which works.

import com.jogamp.opengl.*;
import com.jogamp.newt.opengl.*;
import com.jogamp.newt.awt.*;

import java.awt.*;
import javax.swing.*;

import java.awt.event.*;
import static javax.swing.JComponent.*;

class Hz extends Canvas {
        public Hz() {
                setBackground(Color.BLACK);
        }
}

public class Main {
        public static void main(String[] args) throws Exception {
                GLProfile glprofile = GLProfile.getDefault();
                GLCapabilities glcaps = new GLCapabilities(glprofile);
                GLWindow win = GLWindow.create(glcaps);

                JPanel jPanel1 = new JPanel();
                jPanel1.setLayout(new BorderLayout());
                jPanel1.add(new Button("north"), BorderLayout.NORTH);
                jPanel1.add(new Button("south"), BorderLayout.SOUTH);
                jPanel1.add(new Button("east"), BorderLayout.EAST);
                jPanel1.add(new Button("west"), BorderLayout.WEST);
//                jPanel1.add(new NewtCanvasAWT(win), BorderLayout.CENTER);
                jPanel1.add(new Hz(), BorderLayout.CENTER);

                JFrame jFrame1 = new JFrame("Swing Parent JFrame");
                jFrame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                jFrame1.setContentPane(jPanel1);

                ActionMap am = jFrame1.getRootPane().getActionMap();
                InputMap im = jFrame1.getRootPane().getInputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

                im.put(KeyStroke.getKeyStroke('w'), "eraser");
                am.put("eraser", new AbstractAction() {
                        public void actionPerformed(ActionEvent e) {
                                System.out.println("!!!!!!!!");
                        }
                });


                SwingUtilities.invokeAndWait(new Runnable() {
                   public void run() {
                       jFrame1.setSize(640, 480);
                       jFrame1.validate();
                       jFrame1.setVisible(true);
                   }
                });
        }
}
Reply | Threaded
Open this post in threaded view
|

Re: NewtCanvasAWT + key bindings

gouessej
Administrator
Therefore, it's not a problem of bad use of AWT/Swing. Please fill a bug report.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: NewtCanvasAWT + key bindings

rndfax
Reply | Threaded
Open this post in threaded view
|

Re: NewtCanvasAWT + key bindings

gouessej
Administrator
Maybe you can use GLCanvas in the meantime
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: NewtCanvasAWT + key bindings

rndfax
> Maybe you can use GLCanvas in the meantime

Is it where rendering and input are handled in one thread? Then, no, thanks :)
Reply | Threaded
Open this post in threaded view
|

Re: NewtCanvasAWT + key bindings

Sven Gothel
Administrator
In reply to this post by rndfax
On 12/26/2015 11:23 PM, rndfax [via jogamp] wrote:

> Hello, everyone!
>
> I have jogl-2.2.4 on my system and I'm trying to use NewtCanvasAWT with Swing
> components. For my program I need hotkeys. In the example below pressed keys
> do not trigger action if focus is inside NewtCanvasAWT component - i.e. if I
> click somewhere in the window (not in NewtCanvasAWT) and then press key 'w' I
> will see my message "!!!!!!!!", but when I click mouse on NewtCanvasAWT and
> then press key 'w' I don't see anything.
>
> Could you please help me this problem? How to trigger actions when the focus
> is inside NewtCanvasAWT?
If the child NEWT windows (child of the AWT parent)
is focused, events will be delivered and consumed to it.
Hence it is naturally that AWT won't receive those events anymore.

You may want to add a NEWT key event listener
and either triggering your actions from there
or propagating the events to your AWT framework.

~Sven



signature.asc (828 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: NewtCanvasAWT + key bindings

rndfax
> If the child NEWT windows (child of the AWT parent)
> is focused, events will be delivered and consumed to it.
> Hence it is naturally that AWT won't receive those events anymore.

I thought that NewtCanvasAWT must behave like regular AWT Canvas, found the differences and immediately panic about that. If what you say is what it is by its nature - i.e. events are consumed, deal with it - then okay, I can live with this :) It's just this is not documented (am I right?)...

Thanks!