2

I'm making a simple game and I want to control it using numeric keyboard (including NumLock key). A player should be able to press several keys simultaneously, or press and hold any key.

After NumLock key is pressed, I have to turn NumLock back on through Toolkit.getDefaultToolkit().setLockingKeyState(KeyEvent.VK_NUM_LOCK, Boolean.TRUE) so that VK_NUMPADx keys won't stop responding to presses.

The problem with such an approach is that setLockingKeyState seems to emulate VK_NUM_LOCK press. So keyPressed and keyReleased are called twice once for user input and once to turn NumLock on (if needed). When you press and hold NumLock it switches on and off constantly.

How can I avoid such a behaviour and get NumLock to work like any other key?

grabantot
  • 2,111
  • 20
  • 31
  • If your game can only be controlled using the numeric keypad, a lot of laptop users will not be able to play it. – VGR May 01 '15 at 13:12
  • It uses the whole keyboard, including numeric keypad if available. It's not even half way done yet though. – grabantot May 01 '15 at 14:57
  • Why not have both KeyEvent.VK_KP_LEFT and KeyEvent.VK_NUMPAD4 invoke the same action? Then the state of NumLock doesn't matter. Obviously the same applies for VK_KP_RIGHT and VK_NUMPAD6, etc. – VGR May 01 '15 at 16:00
  • Good idea. But for some reason on my notebook when NumLock is off `VK_NUMPAD4` (and other keys) becomes `VK_LEFT` and not `VK_KP_LEFT`. I'd like to keep arrows for other options. – grabantot May 01 '15 at 16:09
  • 1
    It would seem your notebook (and probably many others) doesn't have a fully functional numeric keypad, then. Which goes back to my first comment: a numeric keypad isn't reliably available. – VGR May 01 '15 at 16:15

0 Answers0