C语言 释放鼠标按钮期间,AMotionEvent_getButtonState为每个按钮返回0

bjg7j2ky  于 2023-03-17  发布在  其他
关注(0)|答案(1)|浏览(196)

我正在尝试在NativeActivity应用程序中获取鼠标事件。
但在AMOTION_EVENT_ACTION_BUTTON_RELEASE的情况下,AMotionEvent_getButtonState对所有内容都返回0
handle_app_input回调中,我有以下内容:

// LOGI is a macro for logging to Logcat

int32_t handle_app_input(struct android_app* app, AInputEvent* event)
{
    int32_t event_type = AInputEvent_getType(event);
    
    switch(event_type)
    {
        case AINPUT_EVENT_TYPE_MOTION:
        {
            int32_t motion_input_action = AMotionEvent_getAction(event);
            int32_t event_pointer_index = (motion_input_action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
            motion_input_action &= AMOTION_EVENT_ACTION_MASK;
            
            switch(motion_input_action)
            {
                case AMOTION_EVENT_ACTION_BUTTON_PRESS:
                {
                    int32_t mouse_button_state = AMotionEvent_getButtonState(event);
                    LOGI("Mouse Button Press: %d", mouse_button_state);
                    
                    break;
                }
                case AMOTION_EVENT_ACTION_BUTTON_RELEASE:
                {
                    int32_t mouse_button_state = AMotionEvent_getButtonState(event);
                    LOGI("Mouse Button Release: %d", mouse_button_state);                   
                    
                    break;
                }
                default:
                    break;
            }
        }
        return 1;
    }
    
    return 0;
}

AMOTION_EVENT_ACTION_BUTTON_PRESS的情况下,AMotionEvent_getButtonState会相应地返回。
但在AMOTION_EVENT_ACTION_BUTTON_RELEASE的情况下,AMotionEvent_getButtonState对所有内容都返回0
当我按下鼠标按钮时,Mouse Button Press: 1被记录
但当我释放鼠标按钮Mouse Button Press: 1没有得到记录!
我得到的是Mouse Button Press: 0
为什么会这样?我该怎么补救?

vxqlmq5t

vxqlmq5t1#

您使用了错误的函数:
AMotionEvent_getButtonState
获取按下的所有按钮的按钮状态。
当一个按钮被释放时,它就不再被按下,所以结果是得到0。
您可能应该改用AMotionEvent_getActionButton
AMotionEvent_getActionButton
获取动作事件的操作按钮。当事件与按钮按下或按钮释放操作关联时,返回有效的操作按钮。
另一种方法是保存AMOTION_EVENT_ACTION_BUTTON_PRESS事件中AMotionEvent_getActionButton的值,并将其与AMOTION_EVENT_ACTION_BUTTON_RELEASE发生时获得的值进行比较:

case AMOTION_EVENT_ACTION_BUTTON_PRESS: {
    // pressed_buttons is a global/instance variable which keeps its value across multiple handle_app_input invocations.
    pressed_buttons = AMotionEvent_getButtonState(event);
    ...
    break;
}
case AMOTION_EVENT_ACTION_BUTTON_RELEASE: {
    int32_t button_state = AMotionEvent_getButtonState(event);
    // Calculate which buttons have gone from being pressed to not pressed.
    int32_t released_buttons = (button_state ^ pressed_buttons) & pressed_buttons;
    pressed_buttons = button_state;
    ...
    break;
}

相关问题