public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_PLUS :
case KeyEvent.VK_NUMPAD8 :
case KeyEvent.VK_UP :
if (e.isControlDown()) incZoom();
break;
case KeyEvent.VK_MINUS :
case KeyEvent.VK_DOWN :
if (e.isControlDown()) decZoom();
break;
default :
// Do nothing
break;
}
}
[java] Problem with the numpad
I want to use Ctrl-+ and Ctrl-- to zoom in and out on my game. To do this I implement a KeyListener with the following method:
Now, when I press Ctrl-uparrow, the game zooms in, just as it should. Likewise it zooms out for Ctrl-downarrow. But when I try the numpad-8 or '+' keys, nothing happens, although a System.out.println shows that the key press is being registered. I am mystified. What KeyCodes should I be using, if not VK_NUMPAD8 and VK_PLUS? Or am I missing something?
You could do something like:
System.out.println("KEYEVENT KEYCODE: "+e.getKeyCode());
and then reference it to the api for the right static.
Also, try using VK_EQUALS instead of VK_PLUS.
System.out.println("KEYEVENT KEYCODE: "+e.getKeyCode());
and then reference it to the api for the right static.
Also, try using VK_EQUALS instead of VK_PLUS.
Ah, so that was it. Turns out it wants VK_ADD and VK_SUBTRACT for the keys next to the numpad. Obvious in hindsight. And the numpad 8 registers as VK_UP unless you have NumLock on, which makes sense when you think about it. Thanks for your suggestion Hops, I should have thought of that myself.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement