[java] Problem with the numpad

Started by
1 comment, last by King of Men 19 years, 1 month ago
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:

   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;
      }
   }

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?
To win one hundred victories in one hundred battles is not the acme of skill. To subdue the enemy without fighting is the acme of skill.
Advertisement
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.
BRING BACK THE BLACK (or at least something darker)
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.
To win one hundred victories in one hundred battles is not the acme of skill. To subdue the enemy without fighting is the acme of skill.

This topic is closed to new replies.

Advertisement