Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android handling key up event while menu is visible

Here is my problem.

I override volume down key for my application to do some specific task while it's pressed for a long time, it works fine normally. I can intercept key down and key up events for that.

The key listener events are written in particular custom component and not in activity where this component is used.

Problem arises when I press menu button while I have pressed the volume down key, and when I release it while menu is shown, I can not intercept its key up event.

Can you explain what is the reason behind it? And if this problem can be solved or not?

Thanks.

like image 960
Prasham Avatar asked Jun 30 '11 06:06

Prasham


1 Answers

When Menu is clicked your window focus change. Focus come to Menu view. Because of this onKeyDown() does not work because onKeyDown wil work if the focus of the screen will be on the view or activity which is on top.

Use the following code to know focus of the screen

@Override
    public void onWindowFocusChanged(boolean hasFocus) {
        // TODO Auto-generated method stub
        System.out.println("....window focus changed..");
        super.onWindowFocusChanged(hasFocus);
    }
like image 145
Sunil Kumar Sahoo Avatar answered Sep 30 '22 12:09

Sunil Kumar Sahoo