Android 11.0 实现组合键 FN + CTRL,返回主页面

△注意:这里有一个坑点。功能我们Framework这边是实现了,但是有时候Ctrl+FN一旦组合使用后,会导致其他按键不正常,经排查是由于Ctrl键一直处于锁定状态(按下状态),为什么呢?我可以告诉你,这是驱动上报事件写的问题,因为我不知道驱动已经写好了这样的事件,导致自己写了一个估计与他们产生了冲突,只要是组合键他只会上报CTRL+FN的组合键值,并不会上报给我CTRL和FN的单个键值,所以导致我们FW层这边的逻辑被打乱没有接收到按键的松开事件,所以就会一直处于按下的状态。

总结:组合键开发时,建议FW工程师来做组合键功能会更好,这样就不会打乱上层正常接收事件的逻辑。

frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java

    //wangrui FN + CTRL key combination status flag
++     private boolean flag2 = false;


public long interceptKeyBeforeDispatching(IBinder focusedToken, KeyEvent event,
                                              int policyFlags) {
    
    

        //wangrui   FN + Ctrl key combination status flag
++        if (down) {
    
    
++            if (keyCode == KeyEvent.KEYCODE_UNKNOWN) {
    
    
++                 flag2 = true;
++             } else if (flag2) {
    
    
++                 int keyValue = event.getKeyCode();
++                 if (keyValue == KeyEvent.KEYCODE_CTRL_LEFT || keyCode == KeyEvent.KEYCODE_CTRL_RIGHT) {
    
    
++                     Intent intent1=new Intent("xxx.allApp");
++                     intent1.putExtra("COMPOSITION",1);
++                     mContext.sendBroadcast(intent1);
++                     return 0;
++                 }
++             }
++         }else {
    
    
++             if (keyCode == KeyEvent.KEYCODE_UNKNOWN) {
    
    
++                 flag2 = false;
++             }
++         }
}

packages/apps/Launcher3/src/com/android/launcher3/Launcher.java

// wangrui Receive "☰" key broadcast, realize pop-up application and return to homepage
    private final BroadcastReceiver myReceiver = new BroadcastReceiver() {
    
    
        @Override
        public void onReceive(Context context, Intent intent) {
    
    
            android.app.ActivityManager am = (android.app.ActivityManager) getSystemService(ACTIVITY_SERVICE);
            android.content.ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
            if ("AllApps".equals(getStateManager().getState().toString()) || (!"com.android.launcher3".equals(cn.getPackageName()))){
    
    
                intent= new Intent(Intent.ACTION_MAIN);
                intent.addCategory(Intent.CATEGORY_HOME);
                startActivity(intent);
            }else{
    
    
+                //wangrui If it is a combination key, nothing will be done
+                if (!(1==intent.getIntExtra("COMPOSITION",0)))
+                getStateManager().goToState(ALL_APPS);
+            }
+        }
    };

我是王睿丶,加入我的Q群:901440630,欢迎一起讨论安卓技术!

猜你喜欢

转载自blog.csdn.net/qq_27494201/article/details/125083489