0

I'm trying to detect a Space click on the Keyboard using KEYBOARD_SPACE and it doesn't seem to detect anything. My code is:

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    switch (keyCode) {
        case KeyEvent.KEYCODE_SPACE:
            return true;
        case KeyEvent.KEYCODE_DEL:
            return true;
        default:
            return super.onKeyUp(keyCode, event);
    }
}

The second case KeyEvent.KEYCODE_DEL works perfectly, so I'm a little unsure what the problem is. Is there a better way of doing this?

Thanks for any help in advance!

Connor McFadden
  • 441
  • 1
  • 4
  • 20
  • [Here](http://stackoverflow.com/questions/23618567/dispatchkeyevent-to-listen-for-spacebar-being-pressed) is a similar question to this. The answer states you shouldn't rely on a key press on a soft keyboard to generate the event. – BradyK Oct 07 '14 at 16:36

1 Answers1

0
import java.awt.event.*;
import javax.swing.*;   



public class KeyboardControls implements KeyListener
{


   public KeyboardControls() 
   {
      super();
   }

   public void keyTyped(KeyEvent e) 
   {


   }

   public void keyPressed(KeyEvent e) 
   {

      doSomething(e);

   }

   public void keyReleased(KeyEvent e) 
   {


   }

   private void doSomething(KeyEvent e)
   {


      int id = e.getID();
      String keyString;

      int keyCode = e.getKeyCode();
      keyString = "" + KeyEvent.getKeyText(keyCode);


      if(keyString.equals("Space"))
      {

         // whatever

      }



   }



}

This is the way I would do it. It checks if it's space and then calls whatever method is inside it.