Keyboard input for a game in Java

后端 未结 2 1818
遥遥无期
遥遥无期 2020-12-03 16:36

I\'m writing a game in Java, right now it\'s Swing + JOGL - a JFrame with a GLCanvas.

I handle input using keyPressed etc. events (jframe.addKeyLi

2条回答
  •  攒了一身酷
    2020-12-03 16:45

    Some of the tips in this article on global event listeners include catching key events with the KeyboardFocusManager, and might help with coming back from losing focus.

    Regarding the 3+ keys, that's going to be tricky since the KeyEvent accounts for modifiers but not for multiple (regular) keys in its API. You might have to manage the press state yourself, in that if you get a KEY_PRESSED you store that key and build up the set of keys that are currently pressed. But if you simply don't get events at all when 3 or more keys are pressed, I'm not sure there's much you can do.

    Edit: Also, the JGame library has a JOGL target. Looking at how it handles key events might help. I know it can handle at least 2 keys simultaneously.

提交回复
热议问题