Some questions about KeyMapper

Started by hytparadisee, May 06, 2007, 09:03:33 AM

Previous topic - Next topic

hytparadisee

Sorry for being lazy, if this question was asked before, please redirect me, thanks. ;D

I want to know in AWTGLCanvas rendering mode, how key mapping is bound to the AWT components. The real problem I got is that once my input focus goes to other components (such as buttons or textfields), the keymapper can no longer poll the keys pressed from the GLCanvas. Even from the AWT side i can gain by the focus (by using Component.requestFocusInWindow()), the registered KeyMapper (through constructor) still doesn't work anymore. I even tried making a new instance of KeyMapper every time the GLCanvas gains the input but that does not work either.

I think this is because I do not know exactly how KeyMappers work or behave. Some helps needed thanks.
Today I finally found a problem to my soluuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuution

EgonOlsen

In AWTGLCanvas-mode, it's simply an AWT KeyListener listening for event on the component. There no further magic or OpenGL specific code involved. I'm not sure why it doesn't work...have to tried to implement your own KeyListener and see if that works better for some reason?

hytparadisee

Ok, i'll give it a try on that then.
Today I finally found a problem to my soluuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuution

hytparadisee

Yup, implementing KeyListener works perfect. :D (So I throw away the keymapper for a moment cos Im using either software or AWTGL ;D)
Today I finally found a problem to my soluuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuution

EgonOlsen

Strange, because the Mapper doesn't do anything else than a KeyListener. How did you instantiate the Mapper? Can you provide me with a test case?

hytparadisee

#5
I don't have time to write a test case atm (the thing is from a quite fat project im working). But the only difference is how I get notified by the key events. For the keymapper, it is polled on every update like:


        public void run() {
            while (!exit) {
                if (!idle) {
                    // blah blah blah                       

                    // I polled my key here using MeyMapper.poll()
                    // but once the focus is lost, it will never return a valid
                    // key stroke except KeyMapper.NONE
                    KeyState ks = keyMapper.poll();
                    // Process it here
                    poll(ks.getKeyCode(), ks.getState());

                    render();
                } else {
                    // sleep for a wile
                    try { Thread.sleep(500); } catch (Exception ex) { }
                }
            }
        }


For my way, I just implement KeyListener, and add it to the canvas


...
Canvas ret = buffer.enableGLCanvasRenderer(IRenderer.MODE_OPENGL);
ret.addKeyListener(this);   // this implements KeyListener

// then process
public void keyTyped(KeyEvent e) {}
public void keyPressed(KeyEvent e) {
    poll(e.getKeyCode(), true);
}
public void keyReleased(KeyEvent e) {
    poll(e.getKeyCode(), false);
}


The latter works when the focus is lost and regained (e.g. i click on a button outside the panel in the same frame then click back again), but unfortunately not the former. Anyway it works for me perfectly at the moment. Perhaps next week I can get time for some test case to reproduce the problem. ;D

oops: almost forgot: I instantiated my KeyMapper like this:

keyMapper = new KeyMapper(this);  // where "this" extends JPanel

I also tried to use:

keyMapper = new KeyMapper(canvas);  // where "canvas" extends AWTGLCanvas??

And I also tried to use:

keyMapper = new KeyMapper(getTopFrame());   // where getTopFrame() traverses the top level frame this canvas resides in

I even tried to destroy and recreate keymapper when the focus is lost and gain, but no matter what, none of the above solution works. Therefore, if I click on something else and click back to the panel again, the keys do not work anymore.
Today I finally found a problem to my soluuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuution

EgonOlsen

This is strange. The KeyMapper is not different from your approach (well, obviously it is...but where?). It adds itself as a listener to the component...exactly like you do. It then listens to pressed and released-events and stores them in a list which is polled when calling poll.
I'll investigate this further when i find the time.

raft

are you sure focus is regained when you click back to canvas or panel ?
i didnt use AWTGLCanvas but that's the case if you use a JPanel. attaching a mouse listener and requesting focus on click may help..

hytparadisee

I forget to mention that I do try to force a change of focus back to the canvas by calling requestFocus() or requestFocusInWindow() when a mouse is clicked on the canvas. That one doesn't work either. And actually i believe this is not really necessary because my own way of directly listening on KeyListener events does not use the above-mentioned methods, the focus is auto gained when the canvas receives a mouse click.
Today I finally found a problem to my soluuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuution

stormp

Hi,

I've just noticed that the Keymapper doesn't seem to work in my applet under linux/firefox.  It works fine under windows/firefox.

Any advise?

Thanks.

S.

EgonOlsen


stormp

no its very idle.  But i think its something silly i've done because after checking an older version i see that it worked on linux.  Just weird that it would work on one platform and not another.  Joys of java :D

Thanks for the quick response.

S.

EgonOlsen

The mapper uses a simple KeyListener internally. Try and see if it works if you are using a KeyListener on your own. It shouldn't, because they should be equal in operation, but if it does, maybe it's a flaw in the mapper somewhere.

stormp

I think it's because I have switched to JApplet.  Works fine as Applet.

raft

maybe a silly suggestion but just make sure your component is focusable and has focus