JGoView onKeyEvent does not fire

If I embed any JgoView within a java.awt.Frame, the JgoView’s keyboard events don’t fire. It’s as though the view doesn’t gain focus. I’ll definitely need to capture keyboard events in my application. Does anyone have any thoughts? I’ve tried forcing focus, but without success…

I’d like to override the JGoView’s onKeyEvent, as described in the JGoUserGuide.

Incidentally, I’m forced to use the java.awt.Frame because I’m using the SWT_AWT bridge.

JGo doesn’t automatically handle any keyboard events. If you want to handle keyboard events, you need to add a keyListener to the JGoView and then add a keyPressed method to your listener. For example, to handle the delete key, you could do:

void jGoView1_keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_DELETE:
jGoView1.deleteSelection();
break;
}
}
This will work regardless of what kind of Frame the JGoView is embedded in.

Yeah, I’ve tried that, but it doesn’t work.

The JgoUserGuide.pdf mentioned the following within the Keyboard Commands section:

“A view can accept keyboard focus and can respond to several keyboard commands by default. Override onKeyEvent to change or augment the default commands. You can control whether there is any default key event handling by setting the KeyEnabled property.”

…but this doesn’t seem to work either. I’ve attempted to implement both approaches in my following code example. The JgoView seems to behave normally with the exception that no keyboard events are firing.

Here’s some very simple code that demonstrates my issue:

import java.awt.Frame; import java.awt.event.KeyEvent; import java.awt.event.KeyListener;

import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;

import com.nwoods.jgo.JGoView;

public class KeyboardTestViewPart extends ViewPart {

@Override
public void createPartControl(Composite parent) {
    Composite composite = new Composite(parent, SWT.EMBEDDED);
    composite.setLayout(new GridLayout());
    Frame frame = SWT_AWT.new_Frame(composite);
    JGoView jGoView = new JGoView() {
        @Override
        public void onKeyEvent(KeyEvent arg0) {
            System.out.println("onKeyEvent:" + arg0.getKeyCode());
        }
    };
    jGoView.setKeyEnabled(true);
    frame.add(jGoView);
    
    jGoView.addKeyListener(new KeyListener() {
        public void keyReleased(KeyEvent e) {}
        public void keyTyped(KeyEvent e) {}
        public void keyPressed(KeyEvent e) {
            System.out.println("keyPressed:" + e.getKeyCode());
        }
    });
}

@Override
public void setFocus() {}

}

It’s as though the JgoViewPart isn't properly gaining focus... or perhaps I'm not using the JgoView correctly?

Do you have any other ideas?

Yes, I think it’s a focus issue. I have tested putting a JGoView in a java.awt.Frame in a Swing application and it is working correctly for me (see code below). If for some reason the JGoView doesn’t get the keyboard events, there’s nothing JGo can do.

package awtframe; import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.nwoods.jgo.*; public class Frame1 extends Frame {
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
JGoView jGoView1 = new JGoView(); //Construct the frame
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
JGoBasicNode n = new JGoBasicNode("test");
n.setLocation(100, 100);
this.jGoView1.getDocument().add(n);
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
this.setLayout(borderLayout1);
this.add(jGoView1, BorderLayout.CENTER);
this.setSize(new Dimension(485, 376));
this.setTitle("Frame Title");
jGoView1.addKeyListener(new Frame1_jGoView1_keyAdapter(this));
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
} void jGoView1_keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_DELETE:
jGoView1.deleteSelection();
break;
}
}
} class Frame1_jGoView1_keyAdapter extends java.awt.event.KeyAdapter {
Frame1 adaptee; Frame1_jGoView1_keyAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void keyPressed(KeyEvent e) {
adaptee.jGoView1_keyPressed(e);
}
}

The JgoView successfully gains focus for mouse events etc, but just not for the keyboard. I can select JgoObjects within the JgoView and the keyboard evens still do not fire.

I was hoping for some advice / any advice your developers may have. It may also be useful for other people to know if the JgoView is unreliable when used within an SWT_AWT bridge.

Our project team and I are recently purchased a license for your JGo product and this is causing an issue.

Any advice would be very helpful and appreciated.

I know this isn’t much confort, but I don’t believe the problem is related to JGo. Any component trying to handle keyboard events in these circumstances would undoubtably have the same problem.

I'm not sure if this is related, but Eclipse bug 228221 is titled "SWT no longer receives key events in KeyAdapter when using SWT_AWT.new_Frame AWT frame". The description is as follows:
More information:
In Eclipse RCP 3.3, we've been using SWT_AWT.new_Frame and an
org.eclipse.swt.events.KeyAdapter on the Composite containing the embedded AWT
frame. This no longer works in Eclipse RCP 3.4 M6 (Ganymede), this is a
regression. (I'm only updating the RCP version, not changing our code.)

This is likely caused by a focus problem. I've seen the keyboard event received
by the SWT KeyAdapter once, usually just after opening the embedded AWT frame,
but thereafter the key events are no longer received. The initial key event
isn't received if I click on the AWT frame first.

Note that mouse events on the embedded AWT frame still work.

I've tried to workaround this problem by adding java.awt.KeyListener to our
embedded AWT frame and also to the JPanel that it contains, but the key events
are still lost.

This issue will prevent us from updating to Eclipse 3.4. Please advise if
there's a workaround or if this can be fixed. Cheers!

Thanks ssmith, I’ll explore that further and post any findings.

Of course, there’s also our JGo SWT version. It’s not as complete as the Swing version, but it would allow you to avoid using the bridge.