I was following Google's Touch Gestures Guide on detecting D-pad key events, but they don't seem to be working.
Here is part of my code:
public final class ResultActivity extends Activity implements AsyncResponse{
[...]
@Override
public boolean onKeyUp(int keycode, KeyEvent event){
if(keycode == KeyEvent.KEYCODE_DPAD_DOWN){
//this doesnt get detected
return true;
}
if(keycode == 4){
//this gets detected
return true;
}
return false;
}//end onKeyUp
}//end activity
Yes, I've also tried onKeyDown.
What could be causing this problem?
This isn't exactly answering your question, but I think it is a good suggestion. I had the same issue when I was trying to detect keyevent
s this way. I could only get two of the events to register correctly. After messing around with it for a couple of days, I decided to look for other options and came upon the GestureDetector
, tried it, and it worked perfectly. You can find the API docs here for the GestureDetector
. I'm also going to give you some code on my own to help out.
This is the way I used the GestureDetector
in my code. At the very top, I declared a private GestureDetector
:
private GestureDetector gestureDetector;
Then, in the onCreate()
method, I call a method that creates the GestureDetector
(there is no reason to do it this way over just creating it in the onCreate()
method - it just looks cleaner this way and is more organized):
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
gestureDetector = createGestureDetector(this);
}
The createGestureDetector()
method looks like this:
private GestureDetector createGestureDetector(Context context) {
GestureDetector gestureDetectorTemp = new GestureDetector(context, new GestureDetector.OnGestureListener() {
//we are creating our gesture detector here
@Override
public boolean onDown(MotionEvent motionEvent) {
return false;
}
@Override
public void onShowPress(MotionEvent motionEvent) {
}
@Override
public boolean onSingleTapUp(MotionEvent motionEvent) { //onTap
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
am.playSoundEffect(SoundEffectConstants.CLICK); //if we tap once, play a click sound
return false;
}
@Override
public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent2, float distanceX, float distanceY) {
return false; //this is the wrong kind of scroll
}
@Override
public void onLongPress(MotionEvent motionEvent) {
}
@Override
public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent2, float v, float v2) { //fling = a single slide
int dx = (int) (motionEvent2.getX() - motionEvent.getX()); //figure out the distance moved horizontally
int dy = (int) (motionEvent2.getY() - motionEvent.getY()); //figure out the distance moved vertically
//if dx > 0, moved forward, if dx < 0, moved backward
//if dy > 0, moved up, if dy < 0, moved down
return true;
}
});
return gestureDetectorTemp;
}
@Override
public boolean onGenericMotionEvent(MotionEvent event) {
if (gestureDetector != null) {
return gestureDetector.onTouchEvent(event);
}
return false;
}
You have to make sure to include that onGenericMotionEvent()
method that I have at the end there. That is what makes sure that your GestureDetector
is notified every time a motion event occurs.
One last thing to note is the returns in the GestureDetector
- you return true to consume the event, or false to not consume it. In other words, if you want to override what happens by default, such as closing the app when you slide down, just go into the onFling()
event, and if dy < 0, return true. This consumes the event and doesn't send it on to other default methods to act upon.