9

I want to start an activity when the user presses the space bar. My code is not detecting when the space bar is clicked.

@Override
public boolean dispatchKeyEvent(KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.KEYCODE_SPACE) {
        Toast.makeText(MainActivity.this, "YOU CLICKED SPACE KEY",
                Toast.LENGTH_LONG).show();
        return true;
    }
    Toast.makeText(MainActivity.this, "Didnt work", Toast.LENGTH_SHORT)
            .show();
    return super.dispatchKeyEvent(e);
};

}

The Tokenizer
  • 1,564
  • 3
  • 29
  • 46

4 Answers4

8

You can either set the listener to a View that has current focus, or use dispatchKeyEvent in your Activity, as seen in this answer.

Both will work.


Answer for your comments: If you've followed my link, you've probably implemented Activity.dispatchKeyEvent(KeyEvent) by now. :-)

The code doesnt register anykey i press, but when i push the back key it toasts for me "didnt work"

Do you happen to be using the emulator? I ask that because the latest SDK seems to have some problems with the emulator keyboard. The special keys (DPAD, HOME, BACK etc.) work, but the onscreen, QWERTY keyboard does not register any presses. The physical keyboard in my laptop won't register any presses either.

Don't ask me why.

And I say that because this week I posted the answer in the link, and it was working fine. The change is that I updated the Android SDK to R20/JB this morning, so I guess it could be a factor.

However, it will just work on a real device. I've just connected a physical keyboard to my tablet (P7510 / Honeycomb 3.2), and it listens to space presses just fine.

In case you're still doubting, here is proof: :-)

enter image description here

Community
  • 1
  • 1
davidcesarino
  • 16,160
  • 16
  • 68
  • 109
2
public abstract boolean onKey (View v, int keyCode, KeyEvent event)

This function can be used to check that which key is pressed.

davidcesarino
  • 16,160
  • 16
  • 68
  • 109
Rishi
  • 330
  • 2
  • 13
1

For this case, what I have done is , I have used the overridden function 'onKey'

@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
{
    if (keyCode == nNeededKeyCode)
    {
        //do something
    }
    return true;
}

p.s. firstly you need to make use of 'OnKeyListener()' to get the key press event

Code_Yoga
  • 2,968
  • 6
  • 30
  • 49
1

Sorry for the copy/paste answer, but this should accomplish what you're attempting.

This will take place when the user presses the "spacebar" and then lets the spacebar go. If you don't want to wait for them to let it go, you can remove my boolean values, and execute your code in the onKeyDown function. Hope this helps.

boolean spacePressed = false;
@Override
public boolean onKeyDown(View arg0, Editable arg1, int arg2, KeyEvent arg3) {
    // TODO Auto-generated method stub
       switch (keyCode) {
               case KeyEvent.KEYCODE_SPACE:
               {
                  spacePressed = true;
                  return true;
                }
               // other cases can go here
    }

    return true;
}// End of onKeyDown



@Override
public boolean onKeyUp(View arg0, Editable arg1, int arg2, KeyEvent arg3) {
   if (spacePressed){
       // start intent

       // make this false again, so other keys won't trigger it
       spacePressed = false;
   }

    return true;
}// End of onKeyUp
RyanInBinary
  • 1,533
  • 3
  • 19
  • 47