Button Press, Registered twice

Hi



I am comign across a strange problem, everytime i press a button, the event handler is called twice.



        KeyInput.get().addListener( new KeyInputListener() {
            public void onKey( char character, int keyCode, boolean pressed ) {
               
               if(character == 'v' && !pressed){
                  
                  renderer.switchVertexDisplay();
                  keyChange =true;
               }
               
                System.out.println( "key: '" + (character != 0 ? ""+character : "\0") + "' (code "+keyCode+") " + (pressed?"pressed":"released") );
            }
        } );




    public void simpleUpdate() {
        InputSystem.update();
        input.update(tpf);
        mouseView.update(tpf);
       
        if(keyChange){
           renderObjects();
           keyChange = false;
        }

            if ( KeyBindingManager.getKeyBindingManager().isValidCommand("toggle_pause", false ) ) {
                System.out.println("P");
            }
    }



In my code, everytime V is pressed, i set a global boolean so my renderer knows when to repopulate the graphics nodes (In this case its calls a function to switch Vertex on or off, depending on previous state). But everytime i press the key, the input listener is called twice, i Can see the vertex removed and than redraw again.

Any suggestions ?

Thanks, Dave

It should be called twice, once for pressing the ke, once for releasing. But it seems you noticed that. Did you probably register that listener more than once?

Hi



The strange thing is that for both response, my System.out.println say they're both releases and I can't find any record of press.



I am using SimpleCanvasImpl btw and to register the initial listeners I am using




   protected Canvas getGlCanvas() {
        if (glCanvas == null) {


           DisplaySystem display = DisplaySystem.getDisplaySystem("LWJGL");
            glCanvas = display.createCanvas(width, height);
 
          
            // add a listener... if window is resized, we can do something about it.
            glCanvas.addComponentListener(new ComponentAdapter() {
                public void componentResized(ComponentEvent ce) {
                    doResize();
                }
            });
           
            if(model == null)
               return null;
           
            input = new InputHandler();

           
            glCanvas.addFocusListener(new FocusListener() {

                public void focusGained(FocusEvent arg0) {
                    ((AWTKeyInput) KeyInput.get()).setEnabled(true);
                    ((AWTMouseInput) MouseInput.get()).setEnabled(true);
                }

                public void focusLost(FocusEvent arg0) {
                    ((AWTKeyInput) KeyInput.get()).setEnabled(false);
                    ((AWTMouseInput) MouseInput.get()).setEnabled(false);
                }


            });

            ((JMECanvas) glCanvas).setUpdateInput(true);

            KeyInput.setProvider( InputSystem.INPUT_SYSTEM_AWT );
            ((AWTKeyInput) KeyInput.get()).setEnabled(false);
            KeyListener kl = (KeyListener) KeyInput.get();

            glCanvas.addKeyListener(kl);

           
           
            MouseInput.setProvider( InputSystem.INPUT_SYSTEM_AWT );
            ((AWTMouseInput) MouseInput.get()).setEnabled(false);
            ((AWTMouseInput) MouseInput.get()).setRelativeDelta(glCanvas);
            glCanvas.addMouseListener((MouseListener) MouseInput.get());
            glCanvas.addMouseWheelListener((MouseWheelListener) MouseInput.get());
            glCanvas.addMouseMotionListener((MouseMotionListener) MouseInput.get());
           
          
            Vector3f up = new Vector3f(0.0f, 0.0f, 1.0f);
           
            impl = new GeometryEngine(model, width, height,glCanvas);
           
           
            JMECanvas jmeCanvas = ( (JMECanvas) glCanvas );
            jmeCanvas.setImplementor(impl);
            jmeCanvas.setUpdateInput( true );
   
           
            //


END OF GL STUFF
        }
        return glCanvas;
    }

Ah, then AWTKeyInput can be the problem… lets have a look:

boolean pressed = ( e.getModifiers() & KeyEvent.KEY_PRESSED ) == 1


This will never be true!

Must be

boolean pressed = e.getID() == KeyEvent.KEY_PRESSED;



Fixed in CVS. Please check if this fixes your problem.

Hi



Yep, that worked beautifully.



Thanks for the help  :smiley: