Default key event handler in an Android Application

Go To StackoverFlow.com

0

Is there a way in Android where an android application when active will catch and process all key events (and maybe touch events) before they are delivered to the actual view which is supposed to be handling it?

I know that we can have onKeyDown or similar method in Activity to handle the keyevent, but it is fired only if none of its child views handles it internally. These view are usually ListView, GridView, ScrollView, etc..

I want to find a way that my keyHandler method is called before that keyEvent is delivered to these views.

Implementation in my keyHandler will be very simple. It will just play a tone upon each event, just like keypress tone, and then forward it to be handled the way it was meant to be by those views or Android framework.

Want to know if its possible beacuse I don't want to write onKeyListener to each and every view in every activity as I have lot of activities and lots of views and it will just become difficult to write the same code everywhere. If there is a way, i can implement that in BaseActivity and derive all my activities by that and go on my way of having default key handler.

2012-04-04 05:08
by Puneet


1

I don’t try it myself but I think this one will help you: (just scroll a little bit down to the method mentioned in the text)

Input Event: Event Handlers - Activity.dispatchTouchEvent(MotionEvent)

Looks like this is the chance to catch events before they get to the window. Read the detailed Description here.

2012-04-04 09:52
by sandkasten
Exactly what I needed. Thanks :) I actually needed it for KeyEvent there was a method for that too. Derived it and am getting the events before the widgets get it - Puneet 2012-04-04 12:24
Ads