无法阅读关键事件。 (纽比)

发布于 2024-12-18 01:38:34 字数 2185 浏览 1 评论 0原文

我正在尝试读取设备上的一些按键以及从触摸屏读取值。触摸屏工作正常,提取值到 _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);
}

谢谢。

I'm trying to read a few of the keys on a device as well as read values from the touch screen. The touch screen is working fine, extracting values to _x and _y and sending them to my renderer class. I thought it'd be a simple and similar procedure to read the keys but I've clearly got it wrong (again). I'm getting no indication that a key press is ever detected. Can anyone help me get this working? And yes I've only begun getting to grips with java so 'nothing' is to obvious :-)

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

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不甘平庸 2024-12-25 01:38:34

用这个

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

use this

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文