2011-11-24 63 views
2

我正試圖讀取設備上的一些按鍵以及從觸摸屏讀取數值。觸摸屏工作正常,提取_x和_y的值並將它們發送到我的渲染器類。我認爲這將是一個簡單和類似的過程來閱讀密鑰,但我清楚地知道它錯了(再次)。我沒有得到任何跡象表明有按鍵被發現。任何人都可以幫助我做到這一點?是的,我纔剛剛開始掌握Java,所以'沒有'是明顯的:-)無法閱讀關鍵事件。 (newby)

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); 
} 

謝謝。

回答

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; 
} 
+0

謝謝,但不會出現任何區別。 – MarbleMad