NewtCanvasAWT + key bindings
Posted by rndfax on Dec 26, 2015; 10:23pm
URL: https://forum.jogamp.org/NewtCanvasAWT-key-bindings-tp4035922.html
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);
}
});
}
}