AruLNadhaN
AruLNadhaN

Reputation: 2826

How to use Onkeydown Event for Buttons?

I am using a KeyEvent in my App.Now I have to replace the KeyEvent With the Button in my Layout.I dont't know how to use OnclickListener with KeyEvent.I searched & got a Method using OnTouchListener.https://stackoverflow.com/a/13311997/2781359

I tried it.I don't know how to use it ???

Now I Want to make the keyEvent Action when the button is Clicked..

My Current Method

public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        int unicode = event.getUnicodeChar();
        if (unicode == 0)
            switch (event.getKeyCode())
            {
                case KeyEvent.KEYCODE_VOLUME_UP:
                    unicode = KeyboardAction.UNICODE_ARROW_UP;
                    break;
                case KeyEvent.KEYCODE_VOLUME_DOWN:
                    unicode = KeyboardAction.UNICODE_ARROW_DOWN;
                    break;
            }
        if (unicode != 0)
        {
            this.application.sendAction(new KeyboardAction(unicode));
        }
        return super.onKeyDown(keyCode, event);
    }

So Help me in the Right Direction :)

Thanks for your Help ...

EDIT

When I Press the Volume UP Button the following action happens

unicode = KeyboardAction.UNICODE_ARROW_UP;

But i Want to Perform this action With a normal button(using OnClickListener) in my layout .

Upvotes: 0

Views: 1423

Answers (1)

RED_
RED_

Reputation: 3007

Do you mean you want a button to act as if one of the buttons has been pressed? I did this in my app to simulate the back button being pressed with the following code:

this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK)); 

Trigger back-button functionality on button click in Android

Upvotes: 1

Related Questions