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.
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.