From 7ccefe4ba737fcef905d773f482b2ef60a6a99db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8C=83=E5=A4=A7=E5=BE=B7?= Date: Mon, 20 Nov 2023 16:59:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=89=A9=E7=90=86=E6=8C=89?= =?UTF-8?q?=E9=94=AE=E7=9B=91=E5=90=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加物理按键监听 --- bin/templates/project/Activity.java | 102 +++++++++++++++++----------- 1 file changed, 61 insertions(+), 41 deletions(-) diff --git a/bin/templates/project/Activity.java b/bin/templates/project/Activity.java index 567b6c72..29f01f30 100644 --- a/bin/templates/project/Activity.java +++ b/bin/templates/project/Activity.java @@ -1,41 +1,61 @@ -/* - Licensed to the Apache Software Foundation (ASF) under one - or more contributor license agreements. See the NOTICE file - distributed with this work for additional information - regarding copyright ownership. The ASF licenses this file - to you under the Apache License, Version 2.0 (the - "License"); you may not use this file except in compliance - with the License. You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, - software distributed under the License is distributed on an - "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - KIND, either express or implied. See the License for the - specific language governing permissions and limitations - under the License. - */ - -package __ID__; - -import android.os.Bundle; -import org.apache.cordova.*; - -public class __ACTIVITY__ extends CordovaActivity -{ - @Override - public void onCreate(Bundle savedInstanceState) - { - super.onCreate(savedInstanceState); - - // enable Cordova apps to be started in the background - Bundle extras = getIntent().getExtras(); - if (extras != null && extras.getBoolean("cdvStartInBackground", false)) { - moveTaskToBack(true); - } - - // Set by in config.xml - loadUrl(launchUrl); - } -} +/* + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. + */ + +package __ID__; + +import android.os.Bundle; +import org.apache.cordova.*; +import android.view.KeyEvent; + +public class __ACTIVITY__ extends CordovaActivity +{ + private static final int[] SCAN_KEYCODE = {520, 521, 522, 523}; + + @Override + public void onCreate(Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + + // enable Cordova apps to be started in the background + Bundle extras = getIntent().getExtras(); + if (extras != null && extras.getBoolean("cdvStartInBackground", false)) { + moveTaskToBack(true); + } + + // Set by in config.xml + loadUrl(launchUrl); + } + + @Override + public boolean onKeyUp(int keyCode, KeyEvent event) { + if (keyCode >= SCAN_KEYCODE[0] && keyCode <= SCAN_KEYCODE[SCAN_KEYCODE.length - 1]) { + } + loadUrl("javascript:cordova.fireWindowEvent('native.onKeyUp',{keycode:"+keyCode+"})"); + return super.onKeyUp(keyCode, event); + } + + @Override + public boolean onKeyDown(int keyCode, KeyEvent event) { + if (keyCode >= SCAN_KEYCODE[0] && keyCode <= SCAN_KEYCODE[SCAN_KEYCODE.length - 1]) { + } + loadUrl("javascript:cordova.fireWindowEvent('native.onKeyDown',{keycode:"+keyCode+"})"); + return super.onKeyDown(keyCode, event); + } + +}