Android 实现单击鼠标右键返回功能

文章介绍了如何在Android系统中实现鼠标右键的特殊功能,包括将右键单击映射为返回功能,屏蔽右键滑动事件,以及添加右键触发菜单的功能。修改了frameworks中的多个文件,如CursorButtonAccumulator.cpp和CursorInputMapper.cpp,添加了AMOTION_EVENT_BUTTON_MENU常量并更新了事件处理逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

AMOTION_EVENT_BUTTON_PRIMARY = 1 << 0,  //左键
/** secondary */
AMOTION_EVENT_BUTTON_SECONDARY = 1 << 1, //右键
/** tertiary */
AMOTION_EVENT_BUTTON_TERTIARY = 1 << 2, //辅助按钮

 1. 实现单击鼠标右键返回功能

 实现单击鼠标右键返回功能

    modified:   frameworks/native/services/inputflinger/reader/mapper/accumulator/CursorButtonAccumulator.cpp

diff --git a/frameworks/native/services/inputflinger/reader/mapper/accumulator/CursorButtonAccumulator.cpp b/frameworks/native/services/inputflinger/reader/mapper/accumulator/CursorButtonAccumulator.cpp
index 168b0a7..197e178 100755
--- a/frameworks/native/services/inputflinger/reader/mapper/accumulator/CursorButtonAccumulator.cpp
+++ b/frameworks/native/services/inputflinger/reader/mapper/accumulator/CursorButtonAccumulator.cpp
@@ -107,7 +107,7 @@ uint32_t CursorButtonAccumulator::getButtonState() const {
     if (mBtnRight) {
         char targetProduct[PROPERTY_VALUE_MAX] = {0};
         property_get("ro.target.product", targetProduct, "");
-        if (strcmp(targetProduct, "box") == 0 || strcmp(targetProduct, "atv") == 0) {
+        if (strcmp(targetProduct, "box") == 0 || strcmp(targetProduct, "atv") == 0 || strcmp(targetProduct, "tablet") == 0) {
             result |= AMOTION_EVENT_BUTTON_BACK;
         } else {
             result |= AMOTION_EVENT_BUTTON_SECONDARY;

2. 屏蔽鼠标右键滑动事件

diff --git a/services/inputflinger/reader/mapper/CursorInputMapper.cpp b/services/inputflinger/reader/mapper/CursorInputMapper.cpp
index edf4573218..7b29de98be 100644
--- a/services/inputflinger/reader/mapper/CursorInputMapper.cpp
+++ b/services/inputflinger/reader/mapper/CursorInputMapper.cpp
@@ -417,7 +417,8 @@ void CursorInputMapper::sync(nsecs_t when, nsecs_t readTime) {
                          mSource, displayId, policyFlags, lastButtonState, currentButtonState);
 
     // Send motion event.
-    if (downChanged || moved || scrolled || buttonsChanged) {
+    if (downChanged || (moved && mCursorButtonAccumulator.getButtonState() != AMOTION_EVENT_BUTTON_SECONDARY) 
+        || scrolled || buttonsChanged) {
         int32_t metaState = getContext()->getGlobalMetaState();
         int32_t buttonState = lastButtonState;
         int32_t motionEventAction; 

3. 添加鼠标右键Menu功能

3.1 定义menu

frameworks/native/include/android/input.h
 
/**
 * Constants that identify buttons that are associated with motion events.
 * Refer to the documentation on the MotionEvent class for descriptions of each button.
 */
enum {
    /** primary */
    AMOTION_EVENT_BUTTON_PRIMARY = 1 << 0,
    /** secondary */
    AMOTION_EVENT_BUTTON_SECONDARY = 1 << 1,
    /** tertiary */
    AMOTION_EVENT_BUTTON_TERTIARY = 1 << 2,
    /** back */
    AMOTION_EVENT_BUTTON_BACK = 1 << 3,
    /** forward */
    AMOTION_EVENT_BUTTON_FORWARD = 1 << 4,
    AMOTION_EVENT_BUTTON_STYLUS_PRIMARY = 1 << 5,
    AMOTION_EVENT_BUTTON_STYLUS_SECONDARY = 1 << 6,
	AMTION_EVENT_BUTTON_MENU = 1 << 7,    //add menu by jimmy
};

 

frameworks/native/services/inputflinger/reader/mapper/TouchCursorInputMapperCommon.h
 
static void synthesizeButtonKeys(InputReaderContext* context, int32_t action, nsecs_t when,
                                 int32_t deviceId, uint32_t source, int32_t displayId,
                                 uint32_t policyFlags, int32_t lastButtonState,
                                 int32_t currentButtonState) {
    synthesizeButtonKey(context, action, when, deviceId, source, displayId, policyFlags,
                        lastButtonState, currentButtonState, AMOTION_EVENT_BUTTON_BACK,
                        AKEYCODE_BACK);
    synthesizeButtonKey(context, action, when, deviceId, source, displayId, policyFlags,
                        lastButtonState, currentButtonState, AMOTION_EVENT_BUTTON_FORWARD,
                        AKEYCODE_FORWARD);
    synthesizeButtonKey(context, action, when, deviceId, source, displayId, policyFlags,
            lastButtonState, currentButtonState, AMTION_EVENT_BUTTON_MENU, AKEYCODE_MENU);//add menu ++ by jimmy
				
}

3.2 实现鼠标右键 menu

framework/native/services/inputflinger/reader/mapper/accumulator/CursorButtonAccumulator.cpp
 
uint32_t CursorButtonAccumulator::getButtonState() const {
    uint32_t result = 0;
    if (mBtnLeft) {
        result |= AMOTION_EVENT_BUTTON_PRIMARY;
    }
    if (mBtnOk) {
       char mKeyMouseState[PROPERTY_VALUE_MAX] = {0};
        property_get("sys.KeyMouse.mKeyMouseState", mKeyMouseState, "off");
        if (strcmp(mKeyMouseState, "on") == 0) {
         result |= AMOTION_EVENT_BUTTON_PRIMARY;
        }
    }
    if (mBtnRight) {
        char targetProduct[PROPERTY_VALUE_MAX] = {0};
        property_get("ro.target.product", targetProduct, "");
        if (strcmp(targetProduct, "box") == 0 || strcmp(targetProduct, "atv") == 0) {
            result |= AMTION_EVENT_BUTTON_MENU; // add menu by jimmy
        } else {
            //result |= AMOTION_EVENT_BUTTON_SECONDARY;
	    result |= AMTION_EVENT_BUTTON_MENU; // add menu by jimmy
        }
    }
    if (mBtnMiddle) {
        result |= AMOTION_EVENT_BUTTON_TERTIARY;
    }
    if (mBtnBack || mBtnSide) {
        result |= AMOTION_EVENT_BUTTON_BACK;
    }
    if (mBtnForward || mBtnExtra) {
        result |= AMOTION_EVENT_BUTTON_FORWARD;
    }
    return result;

}

 

参考文献:

Android12 Framework屏蔽鼠标左/右键滑动事件-优快云博客

鼠标右键不能返回问题 - M-kobe - 博客园 (cnblogs.com)

activity支持鼠标 android 安卓鼠标按键设置_laokugonggao的技术博客_51CTO博客

RK3568 11 鼠标右键Menu功能-优快云博客

短视频系统源代码,remap鼠标右键为返回键-优快云博客

rk3568 android11 鼠标右键点击无法返回_rk3568 鼠标右键后退_Haidern的博客-优快云博客

Android key键值 android 键值表_mob6454cc647bdb的技术博客_51CTO博客

android捕获鼠标输入 android 鼠标_lingjuli的技术博客_51CTO博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值