Jump to content
  • Advertisement
Sign in to follow this  
King of Men

[java] Problem with the numpad

This topic is 5004 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!