Swing and keyboard inputs

Hi all,



I have seen JMESwingTest but somehow I can never make the JMECanvas receive keyboard inputs.

Although the Test does manage to accept mouse click inputs. Is there any way of doing it ? or do I have to write my own handlers ? I tried adding a new inputAction into the InputHandler to no avail, nor does using a KeyBindingManager.

I will appreciate if anyone is able to enlighten me on this point  :smiley:

Or even a way to make a FirstPersonHandler work in the JMESwingTest



Thanks

I have the same Problem. Mouse events are no problem. But i cannot get keyboard events.



Not with the FirstPersonHandler (only mouse is ok if i set requireButtonPressed(true) ), then i tried this:

InputHandler input = new InputHandler();
        input.addAction( new InputAction() {
            public void performAction( InputActionEvent evt ) {
                System.out.println(evt.getTriggerName());
            }
        }, InputHandler.DEVICE_ALL, InputHandler.BUTTON_ALL, InputHandler.AXIS_NONE, false );


That works for mouse, but not for keyboard.
In the jmetest.util.JMESwingTest class the keyboard is mentioned there:

KeyInput.setProvider( KeyInput.INPUT_AWT );


then i read this in javadoc of com.jme.input.KeyInput:
The status of spcific keys can be queried via the isKeyDown
method. For each key that is pressed or released an event is generated which
can be received by a KeyInputListener, these are subsribed via
addListener(KeyInputListener). Handling of events is done inside the
update method.

Does that means that the FirstPersonHandler is not working in this case, because the keyboard events are handled elsewhere ?
I cannot imaging how to get the keyboard events in my jme project, can someone help ?

thank
sebastian

when i try to set

KeyInput.setProvider( KeyInput.INPUT_LWJGL );


in my init method (in my class that implements SimpleCanvasImpl) , i got the following error:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Cannot determine focused state of uncreated window
   at org.lwjgl.opengl.Display.isActive(Display.java:557)
   at com.jme.input.lwjgl.LWJGLKeyInput.update(LWJGLKeyInput.java:101)
   at com.jme.input.InputSystem.update(InputSystem.java:68)
   at com.jmex.awt.lwjgl.LWJGLCanvas.paintGL(LWJGLCanvas.java:91)
   at org.lwjgl.opengl.AWTGLCanvas.paint(AWTGLCanvas.java:308)
   at org.lwjgl.opengl.AWTGLCanvas.update(AWTGLCanvas.java:339)
   at sun.awt.RepaintArea.updateComponent(RepaintArea.java:239)
   at sun.awt.X11.XRepaintArea.updateComponent(XRepaintArea.java:43)
   at sun.awt.RepaintArea.paint(RepaintArea.java:216)
   at sun.awt.X11.XComponentPeer.handleEvent(XComponentPeer.java:683)
   at java.awt.Component.dispatchEventImpl(Component.java:4486)
   at java.awt.Component.dispatchEvent(Component.java:4240)
   at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
   at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
   at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
   at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
   at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
   at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
   at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)



the only option that works is to set it to

KeyInput.setProvider( KeyInput.INPUT_AWT );


in the class that implements jframe (like in the example)

Does that means no keyboard input in general ?

I've quickly updated JMESwingTest to also handle key input.  The whole test is embarrassingly messy source code though…  I'll fix it up one of these days soon.

Hi,

look at JMESwingTest. At the init() method in the SwingFrame class, you add


comp.addKeyListener(new KeyListener(){

            @Override
            public void keyPressed(KeyEvent arg0) {
               // TODO Auto-generated method stub
               
            }

            @Override
            public void keyReleased(KeyEvent arg0) {
               // TODO Auto-generated method stub
               
            }

            @Override
            public void keyTyped(KeyEvent arg0) {
               System.out.println(arg0.getKeyChar());
               
            }
            
         });



If the canvas is focused, the key chars will be given out.

Best,
Andreas

Hi again,

if you want to add key events to the whole application without focusing, you simply add



Toolkit.getDefaultToolkit().getSystemEventQueue().push(
                  new EventQueue(){
                      protected void dispatchEvent(AWTEvent event) {
                                    if (event instanceof KeyEvent) {
                                          KeyEvent keyEvent = (KeyEvent) event;
                                          if (keyEvent.getID() == KeyEvent.KEY_TYPED){
                                                 System.out.println(keyEvent.getKeyChar());
                                              }
                                          }
                                       super.dispatchEvent(event);
                    
                                         }
                  });



Best,
Andreas

WOW, thank you a lot !!! that works great



@renase: thanks for your work. But having key events even if not focus is better in my case. But its also very useful if you fix this !





Sebastian


I ran into this same problem, but I'm pretty sure the AWTKeyInput method is meant to be used on whatever awt component you want the keyboard presses of to change your input.  I didn't word that especially well, but like:



Component comp;

...


KeyInput.setProvider( KeyInput.INPUT_AWT );
comp.addKeyboardListener((AWTKeyInput)KeyInput.get());



Then whenever comp has focus and a key is pressed KeyInput fires off the appropriate command.

If this is correct it'd be cool if even this simple example found it's way into the javadoc for the AWTKeyInput, because it'd be helpful.  Sorry if there's a good way for me to do this; I'm fairly new and unawares if there is  :|.  I don't mean to be asking too much of those in charge.