Author Topic: Some questions about KeyMapper  (Read 7371 times)

Offline hytparadisee

  • int
  • **
  • Posts: 86
    • View Profile
    • http://peterhi.com
Some questions about KeyMapper
« on: May 06, 2007, 09:03:33 am »
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

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11970
    • View Profile
    • http://www.jpct.net
Re: Some questions about KeyMapper
« Reply #1 on: May 06, 2007, 11:56:26 am »
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?

Offline hytparadisee

  • int
  • **
  • Posts: 86
    • View Profile
    • http://peterhi.com
Re: Some questions about KeyMapper
« Reply #2 on: May 06, 2007, 12:18:31 pm »
Ok, i'll give it a try on that then.
Today I finally found a problem to my soluuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuution

Offline hytparadisee

  • int
  • **
  • Posts: 86
    • View Profile
    • http://peterhi.com
Re: Some questions about KeyMapper
« Reply #3 on: May 09, 2007, 09:39:16 am »
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

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11970
    • View Profile
    • http://www.jpct.net
Re: Some questions about KeyMapper
« Reply #4 on: May 09, 2007, 11:21:28 am »
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?

Offline hytparadisee

  • int
  • **
  • Posts: 86
    • View Profile
    • http://peterhi.com
Re: Some questions about KeyMapper
« Reply #5 on: May 09, 2007, 04:29:30 pm »
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:

Code: [Select]
        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

Code: [Select]
...
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:

Code: [Select]
keyMapper = new KeyMapper(this);  // where "this" extends JPanel
I also tried to use:

Code: [Select]
keyMapper = new KeyMapper(canvas);  // where "canvas" extends AWTGLCanvas??
And I also tried to use:

Code: [Select]
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.
« Last Edit: May 09, 2007, 04:35:59 pm by hytparadisee »
Today I finally found a problem to my soluuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuution

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11970
    • View Profile
    • http://www.jpct.net
Re: Some questions about KeyMapper
« Reply #6 on: May 09, 2007, 05:41:30 pm »
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.

Offline raft

  • quad
  • ******
  • Posts: 1986
    • View Profile
    • http://www.aptalkarga.com
Re: Some questions about KeyMapper
« Reply #7 on: May 10, 2007, 03:01:04 am »
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..

Offline hytparadisee

  • int
  • **
  • Posts: 86
    • View Profile
    • http://peterhi.com
Re: Some questions about KeyMapper
« Reply #8 on: May 10, 2007, 07:43:52 am »
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

Offline stormp

  • byte
  • *
  • Posts: 38
    • View Profile
Re: Some questions about KeyMapper
« Reply #9 on: August 02, 2007, 09:23:08 am »
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.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11970
    • View Profile
    • http://www.jpct.net
Re: Some questions about KeyMapper
« Reply #10 on: August 02, 2007, 10:09:31 am »
Are you using the cpu to 100%?

Offline stormp

  • byte
  • *
  • Posts: 38
    • View Profile
Re: Some questions about KeyMapper
« Reply #11 on: August 02, 2007, 10:28:09 am »
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.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11970
    • View Profile
    • http://www.jpct.net
Re: Some questions about KeyMapper
« Reply #12 on: August 02, 2007, 10:49:57 am »
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.

Offline stormp

  • byte
  • *
  • Posts: 38
    • View Profile
Re: Some questions about KeyMapper
« Reply #13 on: August 02, 2007, 10:56:08 am »
I think it's because I have switched to JApplet.  Works fine as Applet.

Offline raft

  • quad
  • ******
  • Posts: 1986
    • View Profile
    • http://www.aptalkarga.com
Re: Some questions about KeyMapper
« Reply #14 on: August 02, 2007, 10:59:08 am »
maybe a silly suggestion but just make sure your component is focusable and has focus