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

问题描述 投票:0回答:1

我正在尝试在 Android NDK 和 Native Activity App 中获取鼠标按钮事件

但是好像没有检测到鼠标按键释放事件!

当我按下任何按钮

AMOTION_EVENT_ACTION_DOWN
部分工作 但是当我释放那个按钮时
AMOTION_EVENT_ACTION_UP
不起作用

Android API 级别为 30

handle_input_event
回调中,我使用:

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

这有什么问题吗?

如何正确操作?

android c android-ndk
1个回答
0
投票

您可以尝试使用 AMotionEvent_getAction 而不是 AKeyEvent_getAction AINPUT_EVENT_TYPE_MOTION

  eventAction = AMotionEvent_getAction(event)
  // pointer index code here
  switch(eventAction & AMOTION_EVENT_ACTION_MASK)
  // cases and stuff
© www.soinside.com 2019 - 2024. All rights reserved.