2

How to mesure how long ex. volume up key has been pressed in DialogFragment?

Unfortunately DialogFragment doesn't have methods onKeyUp, onKeyDown. :((

So who (which object) receives by default information about pressed keys?

I've learnt that I can use getDialog().setOnKeyListener(this) assuming that my class implements OnKeyListener.

Great but then I have the following method:

@Override
public boolean onKey(DialogInterface arg0, int arg1, KeyEvent arg2) {
  // TODO Auto-generated method stub
  return false;
}

But I need onKeyDown, onKeyUp (with possibility to call event.startTracking()

How to achieve this? How to mesure how long ex. volume up key has been pressed (in DialogFragment)?

user2707175
  • 1,133
  • 2
  • 17
  • 44
  • onKey() we get the KeyEvent. Using this you can check if the event is action_up or down and start the task that you want to do. Here is the link for your reference : http://developer.android.com/reference/android/view/KeyEvent.html – AndroGeek Mar 10 '14 at 10:04
  • ok, but I don't have the smallest idea how to do event.startTracking() in that case....? – user2707175 Mar 10 '14 at 12:47
  • Hope this post would be helpful for you: http://stackoverflow.com/questions/8226771/how-to-differentiate-between-long-key-press-and-regular-key-press – AndroGeek Mar 11 '14 at 06:23

1 Answers1

2

You can use

@Override
public boolean onKey(DialogInterface arg0, int arg1, KeyEvent arg2) {
  // TODO Auto-generated method stub
     if (arg2.getAction() == KeyEvent.ACTION_UP){
        // Key up event
     }
     else{
     // Key down event
     }   

  return false;
}
Darci Neto
  • 36
  • 3