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

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

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 回调中,我使用:

  1. // LOGI 只是一个 __android_log_print 的宏
  2. int32_t eventType = AInputEvent_getType(event);
  3. int32_t eventAction = AKeyEvent_getAction(event);
  4. switch(eventType)
  5. {
  6. case AINPUT_EVENT_TYPE_MOTION:
  7. {
  8. int32_t eventPointerIndex = (eventAction & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
  9. eventAction &= AMOTION_EVENT_ACTION_MASK;
  10. switch(eventAction)
  11. {
  12. case AMOTION_EVENT_ACTION_DOWN: // 这部分正常工作并打印所有内容
  13. {
  14. int32_t toolType = AMotionEvent_getToolType(event, eventPointerIndex);
  15. switch (toolType)
  16. {
  17. case AMOTION_EVENT_TOOL_TYPE_MOUSE:
  18. {
  19. int32_t mouseButton = AMotionEvent_getButtonState(event);
  20. if (mouseButton & AMOTION_EVENT_BUTTON_PRIMARY)
  21. LOGI("左键按下");
  22. else if(mouseButton & AMOTION_EVENT_BUTTON_SECONDARY)
  23. LOGI("右键按下");
  24. else if(mouseButton & AMOTION_EVENT_BUTTON_TERTIARY)
  25. LOGI("第三键按下");
  26. break;
  27. }
  28. case AMOTION_EVENT_TOOL_TYPE_UNKNOWN:
  29. default:
  30. break;
  31. }
  32. break;
  33. }
  34. case AMOTION_EVENT_ACTION_UP: // 这不起作用并且不打印任何内容
  35. {
  36. int32_t toolType = AMotionEvent_getToolType(event, eventPointerIndex);
  37. switch (toolType)
  38. {
  39. case AMOTION_EVENT_TOOL_TYPE_MOUSE:
  40. {
  41. int32_t mouseButton = AMotionEvent_getButtonState(event);
  42. LOGI("mouseButton=%d", mouseButton);
  43. if (mouseButton & AMOTION_EVENT_BUTTON_PRIMARY)
  44. LOGI("左键释放");
  45. else if(mouseButton & AMOTION_EVENT_BUTTON_SECONDARY)
  46. LOGI("右键释放");
  47. else if(mouseButton & AMOTION_EVENT_BUTTON_TERTIARY)
  48. LOGI("第三键释放");
  49. break;
  50. }
  51. case AMOTION_EVENT_TOOL_TYPE_UNKNOWN:
  52. default:
  53. break;
  54. }
  55. break;
  56. }
  57. }
  58. }
  59. }

这里有什么问题?

如何正确处理这个问题?

英文:

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:

  1. // LOGI is just a macro for __android_log_print
  2. int32_t eventType = AInputEvent_getType(event);
  3. int32_t eventAction = AKeyEvent_getAction(event);
  4. switch(eventType)
  5. {
  6. case AINPUT_EVENT_TYPE_MOTION:
  7. {
  8. int32_t eventPointerIndex = (eventAction & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
  9. eventAction &= AMOTION_EVENT_ACTION_MASK;
  10. switch(eventAction)
  11. {
  12. case AMOTION_EVENT_ACTION_DOWN: // This works correctly and prints everything
  13. {
  14. int32_t toolType = AMotionEvent_getToolType(event, eventPointerIndex);
  15. switch (toolType)
  16. {
  17. case AMOTION_EVENT_TOOL_TYPE_MOUSE:
  18. {
  19. int32_t mouseButton = AMotionEvent_getButtonState(event);
  20. if (mouseButton & AMOTION_EVENT_BUTTON_PRIMARY)
  21. LOGI("Left Click Press");
  22. else if(mouseButton & AMOTION_EVENT_BUTTON_SECONDARY)
  23. LOGI("Right Click Press");
  24. else if(mouseButton & AMOTION_EVENT_BUTTON_TERTIARY)
  25. LOGI("3rd Click Press");
  26. break;
  27. }
  28. case AMOTION_EVENT_TOOL_TYPE_UNKNOWN:
  29. default:
  30. break;
  31. }
  32. break;
  33. }
  34. case AMOTION_EVENT_ACTION_UP: // This doesn't work correctly and prints nothing
  35. {
  36. int32_t toolType = AMotionEvent_getToolType(event, eventPointerIndex);
  37. switch (toolType)
  38. {
  39. case AMOTION_EVENT_TOOL_TYPE_MOUSE:
  40. {
  41. int32_t mouseButton = AMotionEvent_getButtonState(event);
  42. LOGI("mouseButton=%d", mouseButton);
  43. if (mouseButton & AMOTION_EVENT_BUTTON_PRIMARY)
  44. LOGI("Left Click Release");
  45. else if(mouseButton & AMOTION_EVENT_BUTTON_SECONDARY)
  46. LOGI("Right Click Release");
  47. else if(mouseButton & AMOTION_EVENT_BUTTON_TERTIARY)
  48. LOGI("3rd Click Release");
  49. break;
  50. }
  51. case AMOTION_EVENT_TOOL_TYPE_UNKNOWN:
  52. default:
  53. break;
  54. }
  55. break;
  56. }
  57. }
  58. }
  59. }

What is wrong with this?

How do I do it correctly?

答案1

得分: 1

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

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

You could try using AMotionEvent_getAction instead of AKeyEvent_getAction for AINPUT_EVENT_TYPE_MOTION.

  1. eventAction = AMotionEvent_getAction(event)
  2. // pointer index code here
  3. switch(eventAction & AMOTION_EVENT_ACTION_MASK)
  4. // 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:

确定