无法阅读关键事件。 (纽比)
我正在尝试读取设备上的一些按键以及从触摸屏读取值。触摸屏工作正常,提取值到 _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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
用这个
use this