无法阅读重要事件。 (纽比)

时间:2011-11-24 06:01:01

标签: android keyevent keycode

我正在尝试读取设备上的一些按键以及从触摸屏读取值。触摸屏工作正常,将值提取到_x和_y并将它们发送到我的渲染器类。我认为这是一个简单而类似的程序来读取密钥,但我显然错了(再次)。我没有得到任何关于按键被检测到的迹象。任何人都可以帮我搞定这个吗?是的,我只是开始掌握java所以“没有”显而易见: - )

public class VortexTouchView  extends GLSurfaceView {

private VortexRenderer _renderer;
private float _x1 = -1; private float _y1 = -1;
private float _x0 = -1; private float _y0 = -1;

public VortexTouchView(Context context) {
    super(context);

    // Create the renderer
    _renderer = new VortexRenderer(this);   
}

//  THE TOUCH SCREEN WORKS.
//*************************
public boolean onTouchEvent(MotionEvent event){                
               int action = event.getAction();
               int actionCode = action & MotionEvent.ACTION_MASK;
               for (int i = 0; i < event.getPointerCount(); i++) {
                 if(event.getPointerId(i) == 0){ _x0 =  event.getX(i); _y0 =  event.getY(i); }
                 if(event.getPointerId(i) != 0){ _x1 =  event.getX(i); _y1 =  event.getY(i);  }
                 }     
             if (actionCode == 1 || actionCode == 6 && action != 0){_x1 =-1f; _y1 =0f;} 
             if (actionCode == 1 || actionCode == 6 && action == 0){_x0 =-1f; _y0 =0f;} 
_renderer.touchscreen (_x1,_y1,_x0,_y0);    
return true;    }




//  BUT THE VOLUME KEY (or any other) DOESN'T APPEAR TO BE READ.
//*************************************************************
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN) {

        //do something
        _x1=250f; _y1=250f; _x0=250f; _y0=250f;
         _renderer.touchscreen (_x1,_y1,_x0,_y0);
         // end  of do something

        return true;
    }
    return super.onKeyDown(keyCode, event);
}

//  AND THIS DOESN'T WORK EITHER
//******************************
public boolean onKeyUp(int keyCode, KeyEvent event) {
    if (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN) {

        //do something
        _x1=-1f; _y1=-1f; _x0=-1f; _y0=-1f;
         _renderer.touchscreen (_x1,_y1,_x0,_y0);
         // end  of do something

        return true;
    }
    return super.onKeyUp(keyCode, event);
}

感谢。

1 个答案:

答案 0 :(得分:0)

使用此

public boolean onKeyUp(int keyCode, KeyEvent event) {
boolean X=super.onKeyUp(keyCode, event);
if (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN) {

    //do something
    _x1=-1f; _y1=-1f; _x0=-1f; _y0=-1f;
     _renderer.touchscreen (_x1,_y1,_x0,_y0);
     // end  of do something

    return true;
}
return X;
}