Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8158677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:41:09+00:00 2026-06-06T17:41:09+00:00

I want to start an activity when the user presses the space bar. My

  • 0

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

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T17:41:11+00:00Added an answer on June 6, 2026 at 5:41 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to start an activity indicator whenever the user presses any link on
I'm not able to start activity from thread. I want to start one activity
I am using following code to start activity when user pressing search button on
Ideally, I do not want to start an activity to do this. When the
I want to start an activity when the service is ran for the first
Normally, if I want to start a new activity I can use StartActivity(typeof(foo)); This
I have a phonegap activity which I want to start via a button press:
I have a situation like this: start(); <Some code> end(); I want start() function
I want to start an activity inside a pop-up screen. Are there any suggestions
I want to start camera activity in my android app and I know how

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.