编程语言
首页 > 编程语言> > Java-Android NDK捕获关键事件

Java-Android NDK捕获关键事件

作者:互联网

我有一个Android NDK游戏(NDK 4).几乎所有代码都在C中(它是端口),因此在Java中,我仅有的是一个Activity和一个带有onTouchEvents覆盖的GLSurfaceView.我试图弄清楚如何接收按键事件,以便将它们转发到要处理的本机代码.

我尝试过使用View实现OnKeyListener,但是从未调用过onKey().还尝试覆盖视图中的onKeyDown()和onKeyUp(),但没有成功.我想念什么吗?

更新资料

我目前正在使用的View:

public class FooView extends GLSurfaceView implements SurfaceHolder.Callback, OnKeyListener
{
    private GameRenderer _renderer;
    private GameListener _listener;

    public FooView(Context context)
    {
        super(context);
        this._renderer = new GameRenderer();
        setRenderer(this._renderer);
        this._listener = new GameListener(context);
        BaseLib.setListener(this._listener);
    }

    @Override
    public boolean onTouchEvent(final MotionEvent event)
    {
        // touch code...
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        Log.d("testing", "onKeyDown event from Java");
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event)
    {
        Log.d("testing", "onKeyUp event from Java");
        return super.onKeyUp(keyCode, event);
    }

    @Override
    public boolean onKey(View view, int keyCode, KeyEvent event)
    {
        Log.d("testing", "onKey event from Java");
        return true;
    }
}

解决方法:

尝试这个:

  @Override
  public boolean dispatchKeyEvent(KeyEvent event)
  {
          //
          // SEND event.getAction() to your NDK code
          //
          if(<NDK CODE PROCESSED IT>) return true;

          // default behavior for everything else
          return super.dispatchKeyEvent(event);
  }

标签:android-ndk,java,android
来源: https://codeday.me/bug/20191208/2091675.html