From c3994a1eb136d499712e1b2ff74329da6ddc1851 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=A2=A6=E9=BE=99?= Date: Tue, 8 Jul 2025 07:24:49 +0000 Subject: [PATCH] =?UTF-8?q?add=20AudioKit/entry/src/main/ets/pages/Interce?= =?UTF-8?q?ptUpAndDownButtons.ets.=20FAQ=EF=BC=9A=E5=A6=82=E4=BD=95?= =?UTF-8?q?=E6=8B=A6=E6=88=AA=E7=B3=BB=E7=BB=9F=E9=9F=B3=E9=87=8F=E8=B0=83?= =?UTF-8?q?=E8=8A=82=E7=9A=84=E4=B8=8A=E4=B8=8B=E9=94=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 杨梦龙 --- .../ets/pages/InterceptUpAndDownButtons.ets | 103 ++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 AudioKit/entry/src/main/ets/pages/InterceptUpAndDownButtons.ets diff --git a/AudioKit/entry/src/main/ets/pages/InterceptUpAndDownButtons.ets b/AudioKit/entry/src/main/ets/pages/InterceptUpAndDownButtons.ets new file mode 100644 index 0000000..b0579c4 --- /dev/null +++ b/AudioKit/entry/src/main/ets/pages/InterceptUpAndDownButtons.ets @@ -0,0 +1,103 @@ +/* +* Copyright (c) 2024 Huawei Device Co., Ltd. +* Licensed 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. +*/ +/* +* FAQ:如何拦截系统音量调节的上下键 +*/ + +// [Start Intercept] +import { inputConsumer, KeyEvent } from '@kit.InputKit'; +import { KeyCode } from '@kit.InputKit'; + + +@Entry +@Component +struct TestDemo14 { + private volumeUpCallBackFunc: (event: KeyEvent) => void = () => { + } + private volumeDownCallBackFunc: (event: KeyEvent) => void = () => { + } + + aboutToAppear(): void { + try { + let options1: inputConsumer.KeyPressedConfig = { + key: KeyCode.KEYCODE_VOLUME_UP, + action: 1, // 按下按键的行为 + isRepeat: false, // 优先消费掉按键事件,不上报 + } + let options2: inputConsumer.KeyPressedConfig = { + key: KeyCode.KEYCODE_VOLUME_DOWN, + action: 1, // 按下按键的行为 + isRepeat: false, // 优先消费掉按键事件,不上报 + } + + // 点击了音量按键上事件回调 + this.volumeUpCallBackFunc = (event: KeyEvent) => { + this.getUIContext().getPromptAction().showToast({ message: '点击了音量按键上' }) + // do something + } + + // 点击了音量按键下事件回调 + this.volumeDownCallBackFunc = (event: KeyEvent) => { + this.getUIContext().getPromptAction().showToast({ message: '点击了音量按键下' }) + // do something + } + // 注册监听事件 + inputConsumer.on('keyPressed', options1, this.volumeUpCallBackFunc); + inputConsumer.on('keyPressed', options2, this.volumeDownCallBackFunc); + } catch (error) { + console.error(`Subscribe execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`); + } + } + + build() { + Column() { + Row() { + Button('取消监听音量按键上的监听') + .onClick(() => { + try { + // 取消指定回调函数 + inputConsumer.off('keyPressed', this.volumeUpCallBackFunc); + this.getUIContext().getPromptAction().showToast({ message: '取消监听音量按键上的监听事件成功!' }) + } catch (error) { + console.error(`Unsubscribe execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`); + } + }) + }.width('100%') + .justifyContent(FlexAlign.Center) + .margin({ top: 20, bottom: 50 }) + + Row() { + Button('取消监听音量按键下的监听') + .onClick(() => { + try { + // 取消指定回调函数 + inputConsumer.off('keyPressed', this.volumeDownCallBackFunc); + this.getUIContext().getPromptAction().showToast({ message: '取消监听音量按键下的监听事件成功!' }) + } catch (error) { + console.error(`Unsubscribe execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`); + } + }) + }.width('100%') + .justifyContent(FlexAlign.Center) + .margin({ top: 20, bottom: 50 }) + Row(){ + Text('已默认添加监听音量按键上和下的监听') + } + .width('100%') + .justifyContent(FlexAlign.Center) + }.width('100%').height('100%') + } +} +// [End Intercept] \ No newline at end of file -- Gitee