无法获取Android NDK中的鼠标按钮释放事件

huangapple go评论72阅读模式
英文:

Unable to Get Mouse Button Release Events Android NDK

问题

我正在尝试在Android NDK和本机活动应用程序中获取鼠标按钮事件。

但似乎无法检测到鼠标按钮释放事件!

当我按下任何按钮时,AMOTION_EVENT_ACTION_DOWN 部分有效

但是当我释放该按钮时,AMOTION_EVENT_ACTION_UP 不起作用

Android API级别为30

handle_input_event 回调中,我使用:

// LOGI 只是一个 __android_log_print 的宏

int32_t eventType = AInputEvent_getType(event);
int32_t eventAction = AKeyEvent_getAction(event);

switch(eventType)
{
  case AINPUT_EVENT_TYPE_MOTION:
  {
    int32_t eventPointerIndex = (eventAction & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
    eventAction &= AMOTION_EVENT_ACTION_MASK;

    switch(eventAction)
    {
      case AMOTION_EVENT_ACTION_DOWN: // 这部分正常工作并打印所有内容
      {
        int32_t toolType = AMotionEvent_getToolType(event, eventPointerIndex);

        switch (toolType)
        {
          case AMOTION_EVENT_TOOL_TYPE_MOUSE:
          {
            int32_t mouseButton = AMotionEvent_getButtonState(event);

            if (mouseButton & AMOTION_EVENT_BUTTON_PRIMARY)
              LOGI("左键按下");
            else if(mouseButton & AMOTION_EVENT_BUTTON_SECONDARY)
              LOGI("右键按下");
            else if(mouseButton & AMOTION_EVENT_BUTTON_TERTIARY)
              LOGI("第三键按下");

            break;
          }

          case AMOTION_EVENT_TOOL_TYPE_UNKNOWN:
          default:
            break;
        }

        break;
      }
      case AMOTION_EVENT_ACTION_UP: // 这不起作用并且不打印任何内容
      {
        int32_t toolType = AMotionEvent_getToolType(event, eventPointerIndex);

        switch (toolType)
        {
          case AMOTION_EVENT_TOOL_TYPE_MOUSE:
          {
            int32_t mouseButton = AMotionEvent_getButtonState(event);
            LOGI("mouseButton=%d", mouseButton);

            if (mouseButton & AMOTION_EVENT_BUTTON_PRIMARY)
              LOGI("左键释放");
            else if(mouseButton & AMOTION_EVENT_BUTTON_SECONDARY)
              LOGI("右键释放");
            else if(mouseButton & AMOTION_EVENT_BUTTON_TERTIARY)
              LOGI("第三键释放");

            break;
          }
          case AMOTION_EVENT_TOOL_TYPE_UNKNOWN:
          default:
            break;
        }

        break;
      }
    }
  }
}

这里有什么问题?

如何正确处理这个问题?

英文:

I'm trying to get mouse button events in Android NDK and Native Activity App

But it doesn't seem to detect the mouse button release event!

When I press any button AMOTION_EVENT_ACTION_DOWN part works
But when I release that button AMOTION_EVENT_ACTION_UP doesn't work

The Android API Level is 30

In the handle_input_event callback, I use:

// LOGI is just a macro for __android_log_print

int32_t eventType = AInputEvent_getType(event);
int32_t eventAction = AKeyEvent_getAction(event);

switch(eventType)
{
  case AINPUT_EVENT_TYPE_MOTION:
  {
    int32_t eventPointerIndex = (eventAction & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
    eventAction &= AMOTION_EVENT_ACTION_MASK;

    switch(eventAction)
    {
      case AMOTION_EVENT_ACTION_DOWN: // This works correctly and prints everything
      {
        int32_t toolType = AMotionEvent_getToolType(event, eventPointerIndex);

        switch (toolType)
        {
          case AMOTION_EVENT_TOOL_TYPE_MOUSE:
          {
            int32_t mouseButton = AMotionEvent_getButtonState(event);

            if (mouseButton & AMOTION_EVENT_BUTTON_PRIMARY)
              LOGI("Left Click Press");
            else if(mouseButton & AMOTION_EVENT_BUTTON_SECONDARY)
              LOGI("Right Click Press");
            else if(mouseButton & AMOTION_EVENT_BUTTON_TERTIARY)
              LOGI("3rd Click Press");

            break;
          }

          case AMOTION_EVENT_TOOL_TYPE_UNKNOWN:
          default:
            break;
        }

        break;
      }
      case AMOTION_EVENT_ACTION_UP: // This doesn't work correctly and prints nothing
      {
        int32_t toolType = AMotionEvent_getToolType(event, eventPointerIndex);

        switch (toolType)
        {
          case AMOTION_EVENT_TOOL_TYPE_MOUSE:
          {
            int32_t mouseButton = AMotionEvent_getButtonState(event);
            LOGI("mouseButton=%d", mouseButton);

            if (mouseButton & AMOTION_EVENT_BUTTON_PRIMARY)
              LOGI("Left Click Release");
            else if(mouseButton & AMOTION_EVENT_BUTTON_SECONDARY)
              LOGI("Right Click Release");
            else if(mouseButton & AMOTION_EVENT_BUTTON_TERTIARY)
              LOGI("3rd Click Release");

            break;
          }
          case AMOTION_EVENT_TOOL_TYPE_UNKNOWN:
          default:
            break;
        }

        break;
      }
    }
  }
}

What is wrong with this?

How do I do it correctly?

答案1

得分: 1

你可以尝试使用AMotionEvent_getAction而不是AKeyEvent_getAction来处理AINPUT_EVENT_TYPE_MOTION

  eventAction = AMotionEvent_getAction(event)
  // 指针索引代码在这里
  switch(eventAction & AMOTION_EVENT_ACTION_MASK)
  // 案例和其他内容
英文:

You could try using AMotionEvent_getAction instead of AKeyEvent_getAction for AINPUT_EVENT_TYPE_MOTION.

  eventAction = AMotionEvent_getAction(event)
  // pointer index code here
  switch(eventAction & AMOTION_EVENT_ACTION_MASK)
  // cases and stuff

huangapple
  • 本文由 发表于 2023年2月23日 23:40:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75547070.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定