【Android 13 (T) 增加P1物理按键实现开启和关闭虚拟键盘】

本文介绍了在Android13中,通过新增的物理按键P1实现对虚拟键盘的开关操作,以及开发人员如何在PhoneWindow中处理这一功能,绕过了PhoneWindowManager中获取InputMethodManager服务的问题。
摘要由CSDN通过智能技术生成

Android 13 (T) 增加物理按键实现开启和关闭虚拟键盘

一 .增加物理按键P1

驱动已配置节点,直接上代码

1.system/frameworks/base/data/keyboards/Generic.kl

# key 175 "KEY_MOVE"
# key 176 "KEY_EDIT"
key 177   PAGE_UP
key 178   PAGE_DOWN
key 179   NUMPAD_LEFT_PAREN
key 180   NUMPAD_RIGHT_PAREN
# key 181 "KEY_NEW"
# key 182 "KEY_REDO"
# key 183   F13
# key 184   F14
  key 185   P1
# key 186   F16
# key 187   F17
# key 188   F18
# key 189   F19
# key 190   F20
# key 191   F21
# key 192   F22
# key 193   F23
# key 194   F24
# key 195 (undefined)

2.system/frameworks/base/data/keyboards/qwerty.kl

key 165   MEDIA_PREVIOUS
key 166   MEDIA_STOP
key 167   MEDIA_RECORD
key 168   MEDIA_REWIND

key 142   SLEEP
key 581   STEM_PRIMARY
key 582   STEM_1
key 583   STEM_2
key 584   STEM_3
key 185   P1

  1. system/frameworks/native/include/android/keycodes.h
    /** all apps */
    AKEYCODE_ALL_APPS = 284,
    /** refresh key */
    AKEYCODE_REFRESH = 285,
    /** Thumbs up key. Apps can use this to let user upvote content. */
    AKEYCODE_THUMBS_UP = 286,
    /** Thumbs down key. Apps can use this to let user downvote content. */
    AKEYCODE_THUMBS_DOWN = 287,
    /** Used to switch current account that is consuming content.
     * May be consumed by system to switch current viewer profile. */
    AKEYCODE_PROFILE_SWITCH = 288,
    AKEYCODE_P1= 305 // add key P1 by ysliu 2023.8.16


    // NOTE: If you add a new keycode here you must also add it to several other files.
    //       Refer to frameworks/base/core/java/android/view/KeyEvent.java for the full list.
};

#ifdef __cplusplus
}
#endif

4.system/frameworks/native/libs/input/InputEventLabels.cpp

    DEFINE_KEYCODE(COPY), \
    DEFINE_KEYCODE(PASTE), \
    DEFINE_KEYCODE(SYSTEM_NAVIGATION_UP), \
    DEFINE_KEYCODE(SYSTEM_NAVIGATION_DOWN), \
    DEFINE_KEYCODE(SYSTEM_NAVIGATION_LEFT), \
    DEFINE_KEYCODE(SYSTEM_NAVIGATION_RIGHT), \
    DEFINE_KEYCODE(ALL_APPS), \
    DEFINE_KEYCODE(REFRESH), \
    DEFINE_KEYCODE(THUMBS_UP), \
    DEFINE_KEYCODE(THUMBS_DOWN), \
    DEFINE_KEYCODE(PROFILE_SWITCH), \
    DEFINE_KEYCODE(P1) //add key P1 by ysliu 2023.8.16

// NOTE: If you add a new axis here you must also add it to several other files.
//       Refer to frameworks/base/core/java/android/view/MotionEvent.java for the full list.
#define AXES_SEQUENCE \
    DEFINE_AXIS(X), \
    DEFINE_AXIS(Y), \
    DEFINE_AXIS(PRESSURE), \
    DEFINE_AXIS(SIZE), \
    DEFINE_AXIS(TOUCH_MAJOR), \
    DEFINE_AXIS(TOUCH_MINOR), \
  1. system/frameworks/base/core/java/android/view/KeyEvent.java
    public static final int KEYCODE_DEMO_APP_1 = 301;
    /** Key code constant: Demo Application key #2. */
    public static final int KEYCODE_DEMO_APP_2 = 302;
    /** Key code constant: Demo Application key #3. */
    public static final int KEYCODE_DEMO_APP_3 = 303;
    /** Key code constant: Demo Application key #4. */
    public static final int KEYCODE_DEMO_APP_4 = 304;

    //add key P1 by ysliu 2023.8.16 start
    public static final int KEYCODE_P1 = 305;
    //add key P1 by ysliu 2023.8.16 end

   /**
     * Integer value of the last KEYCODE. Increases as new keycodes are added to KeyEvent.
     * @hide
     */
    @TestApi
    public static final int LAST_KEYCODE = KEYCODE_P1;//add key P1 by ysliu 2023.8.16

    // NOTE: If you add a new keycode here you must also add it to:
    //  isSystem()
    //  isWakeKey()
    //  frameworks/native/include/android/keycodes.h
    //  frameworks/native/include/input/InputEventLabels.h
    //  frameworks/base/core/res/res/values/attrs.xml
    //  emulator?
    //  LAST_KEYCODE

6.system/frameworks/base/core/res/res/values/attrs.xml

        <enum name="KEYCODE_FEATURED_APP_1" value="297" />
        <enum name="KEYCODE_FEATURED_APP_2" value="298" />
        <enum name="KEYCODE_FEATURED_APP_3" value="299" />
        <enum name="KEYCODE_FEATURED_APP_4" value="300" />
        <enum name="KEYCODE_DEMO_APP_1" value="301" />
        <enum name="KEYCODE_DEMO_APP_2" value="302" />
        <enum name="KEYCODE_DEMO_APP_3" value="303" />
        <enum name="KEYCODE_DEMO_APP_4" value="304" />
        <!-- add key P1 by ysliu 2023.8.16 start -->
        <enum name="KEYCODE_P1" value="305" />
        <!-- add key P1 by ysliu 2023.8.16 end -->
    </attr>

    <!-- ***************************************************************** -->
    <!-- These define collections of attributes that can are with classes. -->
    <!-- ***************************************************************** -->

  1. system/frameworks/base/core/api/current.txt
    field public static final int KEYCODE_NUMPAD_LEFT_PAREN = 162; // 0xa2
    field public static final int KEYCODE_NUMPAD_MULTIPLY = 155; // 0x9b
    field public static final int KEYCODE_NUMPAD_RIGHT_PAREN = 163; // 0xa3
    field public static final int KEYCODE_NUMPAD_SUBTRACT = 156; // 0x9c
    field public static final int KEYCODE_NUM_LOCK = 143; // 0x8f
    field public static final int KEYCODE_O = 43; // 0x2b
    field public static final int KEYCODE_P = 44; // 0x2c
    field public static final int KEYCODE_P1 = 305; // 0x131
    field public static final int KEYCODE_PAGE_DOWN = 93; // 0x5d
    field public static final int KEYCODE_PAGE_UP = 92; // 0x5c
    field public static final int KEYCODE_PAIRING = 225; // 0xe1
    field public static final int KEYCODE_PASTE = 279; // 0x117
  1. system/frameworks/base/core/api/test-current.txt
  public final class InputDevice implements android.os.Parcelable {
    method @RequiresPermission("android.permission.DISABLE_INPUT_DEVICE") public void disable();
    method @RequiresPermission("android.permission.DISABLE_INPUT_DEVICE") public void enable();
    method @NonNull public android.hardware.input.InputDeviceIdentifier getIdentifier();
  }

  public class KeyEvent extends android.view.InputEvent implements android.os.Parcelable {
    method public static String actionToString(int);
    method public final void setDisplayId(int);
    field public static final int FLAG_IS_ACCESSIBILITY_EVENT = 2048; // 0x800
    field public static final int LAST_KEYCODE = 305; // 0x131
  }

  public final class KeyboardShortcutGroup implements android.os.Parcelable {
    ctor public KeyboardShortcutGroup(@Nullable CharSequence, @NonNull java.util.List<android.view.KeyboardShortcutInfo>, boolean);
    ctor public KeyboardShortcutGroup(@Nullable CharSequence, boolean);
    method public boolean isSystemGroup();
  }

二.按物理按键P1开启或者关闭软键盘

本来是想在PhoneWindowManager.java里的interceptKeyBeforeQueueing方法进行P1按键逻辑处理,但是PhoneWindowManager里无法获取到InputMethodManager服务,因为IMS服务启动比PWM晚,所以导致无法在PWM里实现通过IMS开启或者关闭软键盘。

最终在PhoneWindow的onKeyDown方法实现此功能。
system/frameworks/base/core/java/com/android/internal/policy/PhoneWindow.java


            case KeyEvent.KEYCODE_BACK: {
                if (event.getRepeatCount() > 0) break;
                if (featureId < 0) break;
                // Currently don't do anything with long press.
                if (dispatcher != null) {
                    dispatcher.startTracking(event, this);
                }
                return true;
            }
            //add by ysliu for show or hide the soft keyboard 2023.8.1 start
            case KeyEvent.KEYCODE_P1: {
                    toggleSoftKeyboard();
                return false;
            }
            //add by ysliu for show or hide the soft keyboard 2023.8.16 end

        }

        return false;
    }

    //add by ysliu for show or hide the soft keyboard 2023.8.1 start
    private void toggleSoftKeyboard() {
        if (mContext != null) {
            InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm != null) {
                imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    }
    //add by ysliu for show or hide the soft keyboard 2023.8.1 end
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值