views:

3129

answers:

6

I would like to create a photo/video capture application.

I have created a CaptureView class which extends SurfaceView and placed it in the main form.

The main form's activity has onCreateOptionsMenu() method which creates a menu. The menu worked fine but then I tried to implement a method onKeyDown:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

    if(event.getAction() == KeyEvent.ACTION_DOWN)
    {
        switch(keyCode)
        {
        case KeyEvent.KEYCODE_CAMERA:
            videoPreview.TakePicture();
            return true;
        }
    }

    return super.onKeyDown(keyCode, event);
}

The menu doesn't appear anymore and the method doesn't catch onKeyDown event.

Does anyone know what could be the reason for this issue?

+1  A: 

Well, in looking at the API documentation the only thing that stands out is that the android:clickable attribute must be set as well as the view being enabled for the onKeyDown(...) method to work.

A: 

Your Activity could be eating the key event. Override onKeyDown in the activity and throw a breakpoint in there.

Also: when you say you've "placed it in the main form" are you using the XML layout or doing in your code?

haseman
A: 

Hey Niko

If you haven't found the answer yet, I was having the same issue...

I found that I was returning true for all events, where I should only have been returning it for the code that I was using. I moved the return true inside the scope of the 'if' and returned false... That brought my menu back!

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
super.onKeyDown(keyCode, event);
     if (keyCode == KeyEvent.KEYCODE_BACK) {
      dba.close();
      Intent result = new Intent("Complete");
      setResult(Activity.RESULT_OK, result);
      finish();
      return true;
     }
     **return false;**
    }

Hope this helps Cheers

+1  A: 

I had a similar problem and solved it by adding

this.requestFocus();
this.setFocusableInTouchMode(true);

in the constructor of my SurfaceView subclass.

Ciryon
A: 

you can try this

this.setFocusable(true);

zhujian
A: 

i solved removing the if statement, like this: @Override public boolean onKeyDown(int keyCode, KeyEvent event) {

            switch(keyCode)
            {
            case KeyEvent.KEYCODE_CAMERA:
                    videoPreview.TakePicture();
                    return true;
            }        return super.onKeyDown(keyCode, event);

}

rrabio