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?

4

0 回答 0