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

  • Home
  • SEARCH
  • 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 8987713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:51:34+00:00 2026-06-15T21:51:34+00:00

All I want to do is programatically add an onKeyDown listener to an existing

  • 0

All I want to do is programatically add an onKeyDown listener to an existing activity. A little bit of context for what I want to do: I want to make a standalone function that handles click events that occur in a mediaplayer.

ie: Let’s pretend I have this class.

public class Main extends Activity
{    
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
            setContentView(R.layout.maingui);
    }

@Override
     public boolean onKeyDown(int keyCode, KeyEvent event) {
         switch (keyCode) {
             case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
        }
        return false;
      }
}

What I would like to do is add a function that adds the onKeyDown Listener through programming.

ie:

    public class Main extends Activity
    {    
    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
                setContentView(R.layout.maingui);

                createListener();
        }

}
public void createListener()
{
    @Override
         public boolean onKeyDown(int keyCode, KeyEvent event) {

             switch (keyCode) {
                 case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
          }
            return false;
    }
}

This obviously doesn’t work, but gives you a good idea of what I’m trying to do.

This is what I’ve tried which doesn’t work.

    public class Main extends Activity
    {    
    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
                setContentView(R.layout.maingui);

            MyKeyClickClass.createListener(this);
    }

}
  • Then Imagine this function in a MyKeyClickClass class

    public static void createListener(Activity act)
        {      
            View testing = act.getWindow().getDecorView().findViewById(android.R.id.content);
    
            testing.setOnKeyListener(new OnKeyListener(){
    
                @Override
                public boolean onKey(View v, int keyCode, KeyEvent event) {
                    // TODO Auto-generated method stub
    
                    String test = "Hi";
    
                    return false;
                }
            });
        }
    

Is this possibly to do? I’m not getting any response to key events doing it this way.

Cheers

  • 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-15T21:51:34+00:00Added an answer on June 15, 2026 at 9:51 pm

    If for whatever reason you can’t just use onKeyDown, couldn’t you just have onKeyDown pass its parameters to another function?

    If you create a myKeyListener class then you can do something like this.

     @Override
     public boolean onKeyDown(int keyCode, KeyEvent event) {
         myKeyListener.onKeyDown(keyCode, event);
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

All I want to do is return the index of the i that is
All I want to know is if there's an easy way to add lines
I need to programatically uninstall all Com port devices. The issue is that these
I have a couple of view controllers that I want all to share a
I want to add padding to all of my buttons, so I subclassed UIButton,
I want to add programatically 4 children to dijit.layout.AccordionContainer . The script runs with
I want to add a new view to my scene that will contain content
I want to add BottomAppBar programatically from code-behind in C#. I got it done
I want to read a folder for all the files and programmatically print them
I want to retrieve declared SVG attributes programmatically. It's all inline content, something like

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.