[SOLVED] Controlling a SimpleApplication by KeyCommands (for Fullscreenmode) from the embedded Swing

Hello Everyone,



we tried for Days. But can´t find the Solution ourself. We are working at Univesity Hamburg (Germany) on a Simulation for Software-Agents (http://jadex-agents.informatik.uni-hamburg.de/) ). We are programming the 3D-Visualation for these Simulation with the JMonkey Engine.



Everything works finde, if we just use the Visualation inside the Application as a Canvas drawn in a Swing JFram as you can see on this Picture:

https://dl.dropbox.com/u/4102513/screen1.JPG



We use an KeyboardFocusManager to toggle into Fullscreen mode. (All the other parts in the Swing Application set to invisible and the Swing/Canwas jFrame with the jMonkey Applicatio Output is set to maximum Screensize. Normally with this Code the Keyevents are working and you don´t need to care about Focus.



[java]KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();

manager.addKeyEventDispatcher(new KeyEventDispatcher()

{

public boolean dispatchKeyEvent(KeyEvent e)

{

if(e.getID() == KeyEvent.KEY_PRESSED)

{

makeFullscreen();

}

return false;

}

});[/java]



In our 2d Application (straight Java 2d) it just works fine. But the SimpleApplication from JMonkey seems to “steal” or “observe” all KeyCommands from Swing.



Is there any Possibility to allow the KeyCommands from Swing if you have a SimpleApplication?



Can we just give the jMonkey inputManager something like the KeyListener from Swing or allow It?



It seems not to work because LWGJL use a native JPanel. Thats could be the problem why the KeyboardFocusManager don´t find the Panel.



Any Ideas how to solve the Problem?

This is a problem I’ve seen a few times and never seen a proper solution to. A work around is to move to a different window and then back to your one. When you do that focus has moved off the 3d view and swing stuff works as normal until you click back on the 3d view.

@zarch said:
[..] A work around is to move to a different window and then back to your one. When you do that focus has moved off the 3d view and swing stuff works as normal until you click back on the 3d view.

hm, but that is a very ugly solution. Not suitable for Users of our Plattform. When you do it this way it is even very difficult to go back from fullscreen.

Yes, I agree. I only use the windowed mode for an internal test viewer thing so it’s not a problem but it would never be acceptable in a finished app.



I’m not sure if anyone ever properly raised a bug report for it. It might be worth you checking and if not then post a bug report and a test case.



I believe there are two different options for swing-embedded mode as well (a lightweight and heavyweight way). It might be worth investigating that and if so trying the other way to see if that has the same problem.

Finally! We Found a “good” Workaround/Solution! maybe someone else find this usefull for future, so i Post our way here:



https://dl.dropbox.com/u/4102513/full.JPG



We just “create” a new SwingKeyEvent in the Monkey Application, and add it to the Canvas where the Application draw it´s Output:



[java]ActionListener actionListener = new ActionListener()

{

public void onAction(String name, boolean keyPressed, float tpf)

{

if(keyPressed && name.equals(“Fullscreen”))

{

JmeCanvasContext context = (JmeCanvasContext)getContext();



event = new KeyEvent(context.getCanvas(), KeyEvent.KEY_PRESSED, EventQueue.getMostRecentEventTime(), 0, KeyEvent.VK_F11, KeyEvent.CHAR_UNDEFINED);



Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent( event );



}[/java]





In our ObserverCenterWindow wie just add the KeyListener on the Component that contains the Canvas and it worked!:



[java]

protected KeyListener fullscreenlistener;



fullscreenlistener = new KeyAdapter()

{



public void keyPressed(KeyEvent e)

{

if(e.getKeyCode() == KeyEvent.VK_F11)

{

makeFullscreen();

}



}

};[/java]



and add / remove it to the fullscreen / non fullscreen panel:



[java] /**

  • Sets the perspective view.

    *
  • @param view the view

    */

    public void setPerspectiveView(Component view)

    {

    int loc = splitpane.getDividerLocation();



    Component oldview = splitpane.getRightComponent();

    if(oldview != null)

    {

    oldview.removeKeyListener(fullscreenlistener);

    }



    splitpane.setRightComponent(view);



    view.addKeyListener(fullscreenlistener);



    splitpane.setDividerLocation(loc);

    }[/java]





    Thanks for Help zarch.
1 Like

You have two options, either use AWT panels (see TestAwtPanels example) or put your key assignments in jME3’s InputManager.

@Momoko_Fan said:
You have two options, either use AWT panels (see TestAwtPanels example) or put your key assignments in jME3's InputManager.

as you can see we choosed the second way but use the SystemEventQueue because we don´t want any dependencies between the Application and it´s SwingEnviroment.

so…now we just need to find out how to kill the Binding from DragToRotate on Left Mouse Button.



Even if we kill it in the InputHandler it´s still there… ^^ but thats another Story.



I marked this Thread is Solved